ios: manage some viewcontroller in navigation controller - ios

In my app I need to manage a navigation controller and move it in these viewcontrollers, so I do it
UINavigationController *navController = (UINavigationController*) [self.storyboard instantiateViewControllerWithIdentifier:#"navigationcontroller"];
[navController addChildViewController:firstViewController];
[navController addChildViewController:secondViewController];
[navController addChildViewController:thirdViewController];
[navController addChildViewController:fourthViewController];
[self presentViewController:navController animated:YES completion:nil];
first problem: navigation open at first fourthviewcontroller, why?
second problem: if from secondviewcontroller i do it to pass at first:
[[self navigationController] pushViewController:self.navigationController.viewControllers[0] animated:NO];
I have a crash that say:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported
why? can you help me?

First question
Every time you use "addChildViewController:", the new controller is added at the top of the stack. The last one inserted, your fourthViewController, is at the top of the stack, so it is shown when you call the method
[self presentViewController:navController animated:YES completion:nil];
Second question
It depends on the pushViewController: method itself. In the Apple documentation the doc said that:
The viewController added cannot be an instance of tab bar controller and it must not already be on the navigation stack.
Your app crashes because self.navigationController.viewControllers[0] is already on navigation stack.

First Problem
You're pushing four view controllers onto the navigation stack. So, your stack, after each step, looks like this:
[navController addChildViewController:firstViewController];
Stack: firstViewController
[navController addChildViewController:secondViewController];
Stack: secondViewController, firstViewController
[navController addChildViewController:thirdViewController];
Stack: thirdViewController, secondViewController, firstViewController
You can see the pattern here. In other words, the fourthViewController is presented because it's on the top of the stack.
Second Problem
As for your second problem, you can't push a view controller onto the stack that already exists in the stack. [[self navigationController] pushViewController:self.navigationController.viewControllers[0] animated:NO]; seems absurd when you consider that fact. You're trying to push something from within the stack to the stack.

Related

Add Animation to View Change

So after a lot of research if finally found the code that allows me to change to another view without giving me any errors:
UIViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:#"gameOverPage"];
[[[[UIApplication sharedApplication] delegate] window] setRootViewController:vc];
The only problem with this code is that there is no animation. I want to somehow add the cross dissolve animation to this if possible.
Another major problem is that it shows the view two times (some times three). So it goes to the second view and then less than a second later, it shows the page again. I know this because iAd is reloaded and when I press a button that goes to another page, it is interrupted by the second page coming up again.
To change the view to another (navigating) you don't need to setRootViewController: set it as root view controller always.
you can use a UINavigationController and set a UIViewController as root, then to change view use pushViewController: method of navigation controller, like
//Pre condition - already a viewController is root view controller of navigation controller.
UIViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:#"gameOverPage"];
[self.navigationController pushViewController:vc animted:YES];
Another way is,
[self presentViewController:vc animated:YES completion:nil];
Read more presentViewController, UINavigationController

'Push segues can only be used when the source controller is managed by an instance of UINavigationController

I am having a problem with this.
In my root view controller I am having a textfield & one button. I am giving a condition like if i entered 0 in textfield then only it should move to next view.
upto here it is working correctly. But now here is problem. Here I am having one button & given navigation to third view controller for that button. here i am getting error as
Terminating app due to uncaught exception 'NSGenericException', reason: 'Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
image ref: http://img12.imageshack.us/img12/8533/myp5.png
and i am giving action for button in first view as below
- (IBAction)Submit:(id)sender {
if([tf.text isEqual:#"0"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SecondViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewControllerID" ];
[self presentViewController:vc2 animated:YES completion:NULL];
}
}
Go To:
Editor--> Embed In --> Navigation Controller, to add Navigation Controller to your initial view
After looking at your screenshot, you either need to
Push your SecondViewController onto the existing navigation controller's stack instead of presenting it modally OR
You need to embed your SecondViewController in another navigation controller and then create and present that navigation controller modally from your SamplesViewController
Either way, SecondViewController needs to be embedded in a navigation controller before you can use Push Segues from it
Either embed your view controller in a Navigation controller or if there is a Navigation controller in the story board mak it the initial view controller
I also faced same problem. My problem was I was using model segue style (rather that push) for one before the current controller because of that I think it broke the Navigation chain before already.
Embed your view controller in a UINavigationController is a good option if there is no UINavigationController in the storyboard. Otherwise you can select the UINavigationController and select the root view controller in the inspector pane, then drag it to the UIViewController you want to use as root. the screenshot is as below:
I solved this by creating a custom UIStoryboardSegue between the UINavigationController and the destination view controller:
-(void) perform
{
assert([self.sourceViewController isKindOfClass:[MyNavigationController class]]);
MyNavigationController *launchController = (MyNavigationController *) self.sourceViewController;
[launchController setViewControllers:#[self.destinationViewController] animated:YES];
}
Try this. It works for me.when you set root view controller to first view and use self.presentViewController ,controller move to next view but instance of navigation controller is only for first view,third view required navigation instance so use self.navigationController instead of presentViewController.
NSString * storyboardName=#"Main";
UIStoryboard *storybord=[UIStoryboard storyboardWithName:storyboardName bundle:nil];
SecondViewController *vc=[storybord instantiateViewControllerWithIdentifier:#"SecondViewControllerID"];
[self.navigationController pushViewController:vc animated:YES];

Memory management - How to show an already instantiated ViewController without creating a new instance

I am having major memory management issues. After small use of the program it will crash running out of memory. I have finally found the cause, every time I create a new ViewController rather than accessing the instance, I am creating a new instance.
So app loads and instantiates the FirstViewController. You click a button which instantiates FilterViewController. From here when going back to FirstViewController I am creating a new instance of this as follows:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName
:#"MainStoryboard" bundle:nil];
FirstViewController *fvc = [storyboard
instantiateViewControllerWithIdentifier:#"FirstViewController"];
fvc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
And repeat process. Any way of presenting the view controller without re-instantiating it? I am close to submitting the app (tomorrow hopefully) so I need to try get this sorted. Thanks!
Here is the presentation of the ViewController.
[self presentViewController:fvc animated:YES completion:nil];
Presenting FilterViewController from FirstViewController
- (IBAction)searchOptions:(id)sender {
FilterViewController *ctrl = [[FilterViewController alloc] init];
[UIView transitionFromView:self.view toView:ctrl.view duration:1 options:UIViewAnimationOptionTransitionCurlUp completion:nil];
self.filterViewController = ctrl;
[self.navigationController pushViewController:self.filterViewController animated:NO];
}
If you're using presentViewController, you get back to the previous view by calling [self dismissViewControllerAnimated:YES];. You would do that in the method where you're currently creating the new controller.
If you are pushing into a navigation controller you would pop from the navigation controller: [self.navigationController popViewControllerAnimated:YES];.
Based on your last update it seems like you don't have a navigation controller and you're just adding the view as a subview and storing the filter view controller. That makes life more complicated really and the correct way to remove it is to setup a delegate relationship so that the filter view controller calls back to the first view controller when it's done and the first controller then transitions the views and nil's the reference.
If you can, change to use a navigation controller properly. You already have half the code, but the first view controller seems to not be in a navigation controller. If you use a nav controller life will be easy...

Navigate View Controllers After Presenting First View

I just changed my app and Im quite confused. It started with the root view that then pushed a second view, there is a button on that view that pushes another view...
So I decided to instead present that second view controller but now the other view cant be pushed from the second.
Code From Root View:
//This works
[[self navigationController] presentViewController:secondViewController
animated:YES completion:nil];
Code From Second View:
//This Does not work
[[self navigationController] pushViewController:locactionView animated:YES];
Edit: Sorry for the lack of detail. No exception is thrown it simply doesn't push the "Location View." Before I presented the view I pushed it and everything worked fine. Also when the view was originally pushed the navigation bar was visible, now that the View Controller is being presented I can't push a view or see the Nav Bar. I hope this helps. I don't know what code I could add because I only changed a single line before the problem occurred.
In your AppDelegate you could initialize your NavigationController with the rootViewController first, and then you could push the viewControllers on the stack, that could solve your problem
YourNavigationController *yourNavigationController = [[YourNavigationController alloc] initWithRootViewController:self.firstViewController];
If you have your hierarchy set up like this:
self.rootViewController = [[RootViewController alloc] initWithNibName:#"RootViewController"
bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.rootViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
You should not have any issues with this stuff. Your issue might be that you are trying to push onto the navigation controller from a view controller you presented modally which you cant do

IOS ViewController navigation path - how to navigate back programatically

I'm using IOS5 Storyboard. My View Controller path is as follows:
tabbarVC --> navigationVC-1 --> tableVC-1 --(via segue push)-> tableVC-2 --(via segue modal)-> navigationVC-2 --> tableVC-3
In the cancel button callback action method in tableVC-3 I call [self dismissViewControllerAnimated:YES completion:nil]; that successfully gets me back to tableVC-2. However when I try to examine the navigation path backwards in the debugger, I don't see a way to access tableVC-2 from navigationVC-2. I expected navigationVC-2 to maintain a link to tableVC-2 or navigationVC-1 but it doesn't seem to. Please see my debugger output below.
Can someone explain the navigation hierarchy and how to traverse the chain backwards programatically?
(gdb) po self
<tableVC-3: 0x6d33340>
(gdb) po (UIViewController*) [self navigationController]
<UINavigationController: 0x6d33560>
(gdb) po (UIViewController*)[[self navigationController] navigationController]
Can't print the description of a NIL object.
(gdb) po (UIViewController*)[[self navigationController] topViewController]
<tableVC-3: 0x6d33340>
(gdb) po (UIViewController*)[[self navigationController] presentingViewController]
<UITabBarController: 0x6b2eba0>
(gdb) po (UIViewController*)[[self navigationController] presentedViewController]
Can't print the description of a NIL object.
(gdb) po (UIViewController*)[[self navigationController] visibleViewController]
<tableVC-3: 0x6d33340>
This is an old question, but just to help anyone else who comes across this issue, there's a single command which'll make your life easier..
[self.navigationController popToRootViewControllerAnimated:TRUE];
Easy when you stumble across the right command, isn't it !
So, supposing you had a series of three screens in a Navigation Controller, and on the third screen you wanted the "Back" button to take you back to the initial screen.
-(void)viewDidLoad
{
[super viewDidLoad];
// change the back button and add an event handler
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(handleBack:)];
}
-(void)handleBack:(id)sender
{
NSLog(#"About to go back to the first screen..");
[self.navigationController popToRootViewControllerAnimated:TRUE];
}
After some research using this and a couple other questions for modal UIViewControllers in storyboard to go back two views I used
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
Going throw an old response tough of updating it to be more complet.
To address this question :
Can someone explain the navigation hierarchy and how to traverse the chain backwards programatically?
The structure of your navigation :
tabbarVC --> navigationVC-1 --> tableVC-1 --(via segue push)-> tableVC-2 --(via segue modal)-> navigationVC-2 --> tableVC-3
Can be explain like this :
The TabbarVC is showing it's 'selectedViewController' (navigationVC-1).
NavigationVC-1 has its navigation stack compose of TableVC-1 and TableVC-2 (topViewController of NavigagtionVC-1)
Then NavigationVC-2 is presented Modally over the tabbarVC, so tabbarVC is the presentingViewController and NavigationVC-2 is the presentedViewController
So in order to reach tableVC-2 from tableVC-3 you would need to do something like this :
[(UINavigationController *)[(UITabBarController *)[tableVC-3 presentingViewController] selectedViewController] topViewController];
(don't do that in production code)
[tableVC-3 presentingViewController] as well as [tableVC-3.navigationController presentingViewController] will give you back the UITabBarController.
If you are using a UINavigationController you should use it's push and pop method to put UIViewController on or off the "presentation stack".
You will be able to access the UINavigationController from those UIViewController like this:
self.navigationController
If you want to go back more than one UIViewController on the "presentation stack" you can use this method on the UINavigationController
popToViewController:animated:
Pops view controllers until the specified view controller is at the top of the navigation stack.
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
To dismiss a UIViewController that was presented modally, the UIViewController that have presented it need to dismiss it with :
- (void)dismissModalViewControllerAnimated:(BOOL)animated
So in this case it should be :
[tableVC-2 dismissModalViewControllerAnimated:YES];
Swift
If you are using a navigation controller the you can navigate back to the previous view controller with
self.navigationController?.popViewControllerAnimated(true)
or back to the root view controller with
self.navigationController?.popToRootViewControllerAnimated(true)

Resources