pushViewController don´t work on iOS 5 to launch GTMOAuth2ViewControllerTouch - ios5

I am developing an iPad application using sdk 5 and storyboard. I´m following the [OAuth2] Tutorial and sample.1
I have port the code to a Singleton. Create a new view everytime initSesion is called:
viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope
clientID:kMyClientID
clientSecret:kMyClientSecret
keychainItemName:kKeychainItemName
delegate:self
finishedSelector:#selector(viewController:finishedWithAuth:error:)];
//viewController.keychainItemAccessibility = kSecAttrAccessibleAlways;
NSDictionary *params = [NSDictionary dictionaryWithObject:#"en"
forKey:#"hl"];
//viewController.signIn.additionalAuthorizationParameters = params;
NSString *html = #"<html><body bgcolor=silver><div align=center>Loading sign-in page...</div></body></html>";
viewController.initialHTMLString = html;
[[self navigationController] pushViewController:viewController
animated:YES];
But, when another Viewcontroller call the simgleton, the view pushed is not display.
Teorically, a navigator Controller use a stack to show differents view, so, if i´m in the correct way, that function must launch the GTMOAuth2ViewControllerTouch.view, to log in using OAuth2, thats occur in the sample. But nothing happen.
What´s going on? Thanks in advance.

Check if you have a navigation controller, it could be nil. Use debug mode or put a NSLog:
if(![self navigationController]){ NSLog(#"I´m Nil"); } else { ... }
If you have no created a navigationController, try use the singleton of the app to get it, something like:
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:viewController animated:YES];

Related

Call method in viewDidAppear

I've set up a void method which is called from another class if the user touches the button.
However, I got this error with "whose view is not in window hierarchy" and then I found this: whose view is not in the window hierarchy. I just want to call the method below but I got this error and it doesn't show me the navigatoncontroller. It says Warning: Attempt to present <UINavigationController: 0xae34f10> on <ViewController: 0xae1f200> whose view is not in the window hierarchy!
I don't know if this is possible to call the method from the viewDidAppear or from somewhere that it only works if the button was tapped on the other view before. If I call a NSLog in the method it shows me the NSLog in the output. Only the presenting of the navController doesn't seems to work. I would be happy if someone could help me with this.
My method:
- (void) launchGalleryView
{
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set browser options.
browser.wantsFullScreenLayout = YES;
browser.displayActionButton = NO;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:browser];
NSMutableArray *photos = [[NSMutableArray alloc] init];
MWPhoto *photo;
photo = [MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"calculator" ofType:#"jpg"]];
photo.caption = #"The calculator is soo beauteful...";
[photos addObject:photo];
self.photos = photos;
[self presentModalViewController:navController animated:YES];
}
Just forgot to say: The MWPhotoprowser is a class which actually does't matter.
Thanks in advance.
The error message is pretty clear -- you're trying to present the controller from this class, but the class that has the button you pressed is the one whose view is in the view hierarchy. You either need to present it from the class with the button, or get back to this class however you're doing that, and then call it after viewDidAppear.
i checked your project.. wasn't able to sort out the proper issue..
but i tried a hack and it worked..
replace this line with
[self presentModalViewController:navController animated:YES];
this
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:navController animated:YES];

presentModalViewController - ViewController automatically disappears after presenting

I am currently developing an iPhone App for my company and i ran into a strange thing.
My View Hierachy looks like this:
UITabBarController containing 5 Tabs with every Tab containing a UINAvigationController.
So far everything works perfect.
Now i want to present a modal View controller via the presentModalViewController method on the
UITabBarController using this lines of code:
-(void)callFilterOptions
{
FilterOptionsView *filterView = [[FilterOptionsView alloc] init];
[filterView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self.tabBarController presentModalViewController:filterView animated:TRUE];
}
The FilterOptionsView is a normal UIViewController containing nothing but a black background at the
moment.
What happens is that the view is presented and after a couple of seconds misteriously disappears.
Why is this happening? At no Point i am calling the dismissModalViewController method.
I already ran into this problem when presenting a mailcomposer.
greetings,
Florian
UINavigationController *myNavController = [self navigationController];
[myNavController presentModalViewController:filterView animated:TRUE];
or a better approach might be:
UIApplication *myApp = [UIApplication sharedApplication];
noIBAppDelegate*appDelegate = (noIBAppDelegate*)myApp.delegate;
[appDelegate.tabBarController presentModalViewController:filterView animated:YES];
to dismiss:
UIApplication *myApp = [UIApplication sharedApplication];
noIBAppDelegate*appDelegate = (noIBAppDelegate*)myApp.delegate;
[appDelegate.tabBarController dismissModalViewControllerAnimated:YES];
p.s. I recommend not naming a view controller "filterView" it would be better named "filterViewController"

Sending Variables Between iPhone Controllers

I am currently in the process of switching from Android to the iPhone SDK. I have a TableView where the user selects an item. I am having trouble passing data between controllers. Is there an equivalent to the Android's startActivityForResult or putting extra's into Intents? Like so ...
Intent i = new Intent(this, Foo.class);
i.putExtra("Foo", foo);
Take this in .h file in ParentViewController
NSString *strABC;
Make below function in ParentViewController
-(void)setString:(NSString *)strEntered{
strABC=strEntered;
}
Now In Post view controller do like this:
ParentViewController *objSecond = [[ParentViewController] initwithNibName:#"parentView.xib" bundle:nil];
[objSecond setString:#"Comment Controller"];
[self.navigationController pushViewController:objSecond animated:YES];
[objSecond release];
Now, In secondViewController viewWillAppear Method write this.
-(void)viewWillAppear:(BOOL)animated{
lblUserInput.text = strABC;
}
Please check spelling mistakes as I hand written this. Hope this help.
If you are not using navigationContoller then you can do something like this.
SecondViewControler *objSecond = [[SecondViewController] initwithNibName:#"secondview.xib" bundle:nil];
[objSecond setUserInput:txtUserInput.text];
[objSecond viewWillAppear:YES];
[self.view addSubview:objSecond];
[objSecond release];

access methods and variables in first navigation controller from pushed view

I normally use UIViews to make my apps - but this one I am using a navigationcontroller. I am pushing a view to the top where I want to add items to an array. However, I cannot access the main navigation controller methods etc. Here's the set up
1) AppDelegate adds navigation controller
[window addSubview:navigationController.view];
2) In RootViewController I push a new UIView
AddNewViewController *addNewViewController = [[AddNewViewController alloc] init];
[self presentModalViewController: addNewViewController animated:YES];
3) From the AddNewViewController I then want to access the main RootViewController - but cannot seem to access anything. All the methods etc are declared as I've done it before (using UIViews). This code cannot find anything in the RootViewController.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navigationController myFunction];
I have myFunction in the RootViewController.h file. I have used this method before, but never with a navigation controller. I guess it's something to do with hte stack - but I cannot find out what I've done wrong!
Help is much appreciated!!
* UPDATE *
I have now used
AddNewViewController *addNewViewController = [[AddNewViewController alloc] init];
[self.navigationController pushViewController:addNewViewController animated:YES];
to push the view controller. In my code, I am trying to access the tableview to reload it with the following code
[self.navigationController.mainTableView reloadData];
but the mainTableView is not accessible. I have declared and sync'd it but still cannot see it. I also tried to loop through and use the element[0] of the stack (as below) but didn't get anywhere with that either!
NSArray *controllers = [[NSArray alloc] initWithObjects:self.navigationController.viewControllers, nil];
UIViewController *tmpController = [[UIViewController alloc] init];
tmpController = [controllers objectAtIndex:0];
First of all,
[self presentModalViewController: addNewViewController animated:YES];
does something different than
[self.navigationController pushViewController:addNewViewController animated:YES];
The names of those methods should speak for themselves.
And appDelegate.navigationController is not your rootViewController. It is the UINavigationController. You can get an NSArray of viewControllers attached to the navigationcontroller with self.navigationController.viewControllers. Item at index 0 should be your rootViewController.
If you push the viewController on the navigationControllers stack there is no need to use the appdelegate. You can access the navigationController with self.navigationController.

Get 'EXC_BAD_ACCESS' signal when invoking [UINavigationController pushViewController]

In order to speed up my app, I've create three different UIViewController in AppDelegate and it has readonly property for the controllers. Those controllers are used for navigation controller.
If I tap a button on the root view, I just show another view using pushViewController method. Let me show you some code for this here.
UIViewController* controller = delegate.anotherViewController;
[delegate.navigationController pushViewController:controller animated:YES];
At first time, this work well, but if I navigate back and tap the button again, I've got a signal 'EXC_BAD_ACCESS' at second line.
What's wrong? And, how can I prepare all of my view controllers at the beginning, not create them when they are needed?
Most of the time EXC_BAD_ACCESS means that you've released an object and you're trying to reuse it without retaining it.
Look if you have released your viewController too early and whether you are (re)using it the right way or not...
I had the same problem. My code was
AddMedia *info = [[AddMedia alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:info animated:YES];
[info release];
I was releasing my viewCOntroller which was crashing the app.
When I commented that line, It worked seamlessly. The code after the change is:
AddMedia *info = [[AddMedia alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:info animated:YES];
// [info release];