Push view from modal view and then popToRootView - ios

Here are my ViewControllers as fallows(objects):
FirstViewController - view with tab bar + navigation bar, also part of the ta bar;
SecondViewController - view only with navigation controller
ThirdViewController - view only with navigation controller
And what I am trying to do(logical steps):
present SecondViewController from FirstViewController (modal)
push ThirdViewController from SecondViewController (push)
popToRootViewControllerAnimated - to pop from ThirdViewController to FirstViewController (pop)
And here is the code that I am using by steps:
in FirstViewController class
SecondViewController * secondViewController = [[UIStoryboard MainStoryboard] instantiateViewControllerWithIdentifier:NSStringFromClass([SecondViewController class])];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: secondViewController];
[self.navigationController presentViewController: navigationController animated:YES completion:nil];
in SecondViewController class:
ThirdViewController * thirdViewController = [[UIStoryboard MainStoryboard] instantiateViewControllerWithIdentifier:NSStringFromClass([ThirdViewController class])];
[self.navigationController pushViewController: thirdViewController animated:YES];
and in ThirdViewController class I do:
[self.navigationController popToRootViewControllerAnimated:YES];
My issues is on point 3, when I do the pop to root view controller instead of going from ThirdViewController to FirstViewController it only goes to SecondViewController.

In step 1 you created new UINavigationController instance and you set secondViewController as the rootViewController for it. So now in step 2 when you are pushing the thirdViewController it will get added to the navigation stack of secondViewController.
Finally in step 3 when you are calling "popToRootViewControllerAnimated" it will pop to secondViewController, since secondViewController is the rootViewController of the navigation.
To go to FirstViewController call "dismissViewControllerAnimated" on self.navigationController.
Please refer the below code.
[self.navigationController dismissViewControllerAnimated:YES completion:nil];

Forget about the frist step. Even though you are presenting a view controller modally, you are making it a root view controller till you dismiss it.
You are pushing it from second VC nav controller.
If you pop it, you will go back to second VC, as the third VC is pushed on second nav controller.
If you want to go to your first View controller,present the first VC again in the third VC by
[self presentViewController:firstVC animated:YES completion:nil];
or you can dismiss
[self.navigationController dismissViewController animated:YES completion:nil];
Please note that you cannot have multiple navigation controllers in the reference. You can have only one at any time.
Even though you present the second VC by from first VC nav controller
[self.navigationController presentViewController:secondVC animated:YES completion:nil];
you are presenting the second VC navigation controller here, so in the first VC navigation stack another nav controller will be added. At this point, the second VC navigation controller will be in reference when you call self.navigationController in the secondVC

Related

IOS/Objective-C/Storyboard: Prevent ViewController From Launching Modally

I want a viewcontroller to launch using a show transition, not modally from the bottom. Normally when I use the following code that's what happens. However, in this case, it is launching as a modal controller from the bottom up. Is there a switch I don't know about or could something be set in Storyboard that is causing this VC to launch modally from the bottom instead of showing?
UIStoryboard *storyBoard = self.storyboard;
IDImportEventsOnboard *importEvents =
[storyBoard instantiateViewControllerWithIdentifier:#"importEventsOnboard"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: importEvents];
[self presentViewController:nav animated:YES completion: nil];
The VC is embedded in a navigation controller.
Should I be using showViewController directly to the targetVC without going through the Nav? Or a pushViewController What is a proper, robust way to show a VC with a show transition?
Thanks in advance for any suggestions.
In the above code you are 'presenting' a new NavigationController from a ViewController. In order to do a push/show transition, that needs to be done on an instance of a NavigationController. If your current ViewController is already in a NavigationController, you can push the new ViewController onto the current NavigationController stack. For Example:
UIStoryboard *storyBoard = self.storyboard;
IDImportEventsOnboard *importEventsVC =
[storyBoard instantiateViewControllerWithIdentifier:#"importEventsOnboard"];
[self.navigationController pushViewController:importEventsVC animated:YES];

Push to a UIViewController from a presentViewController

I want to push to a new viewcontroller from a presented viewcontroller. I don't want to dismiss the presented viewcontroller. I want the new viewcontroller to come over the presented viewcontroller.
Can anybody tell me how to do that.
You do not push form the presented controller so the best option is
First you have to dismiss the controller without animation and then in the method of -(void)viewWillApper you can easly push to the controller where you want to push.
You can do by using UINavigationController like
UINavigationController *vcObject = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"YourViewController"];
[self.navigationController presentViewController:vcObject animated:YES completion:NULL];
Now you can easily push or pop to other ViewController like you want.Thankyou
Open your modalView controller as a new rootViewController :
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:nextViewController];
[self.navigationController presentViewController: navController animated:YES completion:nil]

How can I get UINavigationController.pushViewController to push on a presented viewController

I have initialized UINavigationController - nc with VC0
I am in VC0
In VC0, I want to present a "VC1" bottom-to-top
In VC0, [self.nc presentViewController:VC1]
Now I am in VC1
In VC1, I want to present VC2 right-to-left
In VC1, I tried [self.nc pushViewController:VC2]
But it does not work
How can I accomplish the above?
VC1 needs to be in its own UINavigationController. If you put a breakpoint at the point you try to call pushViewController:animated:, you'll notice that the navigation controller property on VC1 is nil.
When you present VC1 do this instead:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc1];
[self.navigationController presentViewController:navigationController completion:nil];
You can then do your pushViewController:animated: call.

iOS: ContainerView -- Externally calling prepareForSegue in childController

In the following setup, Button 1 calls First VC and Button 2 calls Second VC
Currently, I use the following code to call SecondVC by tapping Button 2.
MainVC.m
#implementation MainVC()
....
UINavigationController *navController = [self.childViewControllers objectAtIndex:0];
SecondVC *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:#"second"];
[navController pushViewController:secondVC animated:YES];
The code above displays SecondVC just fine. However, in the First VC there are statements that need be executed before "Second VC" is presented, that are being skipped!
I execute these statements in First VC inside -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender. When the Next Button is tapped in the First VC, all the statements execute because it triggers prepareForSegue before presenting SecondVC. This step is skipped when the code above is used.
Extra Info:
A) Though a Navigation Controller, Main VC in my app does not show the navigation bar in my app. I believe this would be against HIG to display two navigation bars on an iPhone. I show it here to identify Main VC.
B) There are textFields in FirstVC that User fills out. If I just copy the code, will it read and save the data from textFields.text?
Question: How can I call prepareForSegue from First VC when Button 2 is tapped in MainVC? or is there another approach to this?
You could try something like the following:
UINavigationController *navController = [[self childViewControllers] objectAtIndex:0];
UIViewController *vc = [navController topViewController]
if ([vc isKindOfClass:[FirstVC class]]) {
FirstVC *firstVC = (FirstVC *)vc;
SecondVC *secondVC = [[self storyboard] instantiateViewControllerWithIdentifier:#"second"];
// Copy and read data from [[firstVC textField] text] to secondVC.
[navController pushViewController:secondVC animated:YES];
}

segue on destination view controller not working after programmatically going to destination view controller

segues not working after going to destination view controller (which is in storyboard )programmatically from a button click of view controller (which is not in storyboard)
- (IBAction)OK:(id)sender {
UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
mainViewController *controller = (mainViewController *)[myStoryboard instantiateViewControllerWithIdentifier:#"mainViewController"];
[controller setModalPresentationStyle: UIModalPresentationFormSheet];
[self presentModalViewController:controller animated:YES];
}
Okey, if i am not wrong, i think , the button click segue is using navigationcontroller , if so , watch your custom segue ,
[self presentModalViewController:controller animated:YES];
you are making an model segue, so your navigation controller will be unknown after the segue is done.

Resources