I am using Storyboards for an iOS 7 App. The root controller is a menu. For almost every view, I have a Menu button which brings the App back to that menu using [self.navigationController popToRootController:TRUE]. However, there are two UIView elements where I would like to clear the Navigation Controller view list (as happens when you pop back to the root controller), but then immediately go to another UIView without having the user see the root controller's view. Once at this new view, if the user presses the back button, I want them to go to the menu view.
I've tried to put a performSegue in the viewWillAppear, but it really messes up the Navigation Controller and views. I've tried putting a performSegue in the viewDidAppear, but the user sees first the Menu view flash in, then out on it's way to the correct view.
I hope I've explained this well enough. I hope this can be done.
Thanks.
Your best bet is to build the navigation controller stack yourself, and then use - (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated: to replace the current stack.
So you could do something like
...
UIViewController *vcToPush = ...;
NSArray *newVCStack = #[[self.navigationController.viewControllers firstObject], vcToPush];
[self.navigationController setViewControllers:newVCStack animated:YES];
This will add your new controller to the stack using the standard push animation (or not if you so choose), and after the animation is complete, set the view stack to that of the array.
Related
Im working on an App for iOS, thats uses the navigationController to switch betweens views. I'm trying to return from to the root viewcontroller from the third view.
I have succeeded in the Main.Storyboard, with dragging a button from third to root view, but then the NavigationController just continues the stack.
Is it possible to make a command from third view, to return to root ViewController, without the NavigationController Bar showing the "Back" button and keeping track and without reseting any Bools.
If you want to hide the back button from the navigation bar.Then write the code in third view's viewDidLoad or in viewWillAppear-
self.navigationItem.hidesBackButton=YES;
And Now write the code in the body of the action button.Such as-
[self.navigationController popToRootViewControllerAnimated:YES];
Let me know if it works for you.Thank you
You can use the [UINavigationCobtroller popToRootViewControllerAnimated:] to close all the view hierarchy to the first but for the remaining issues you can find plenty of answers on SO.
In my iOS project I have a main menu that is shown embedded in a container in my initial UIViewController.
After the user choses any row in that menu, the navigation controller pushes the submenu viewController that manages further actions, which uses the full window.
If the user wants to go back to main screen, he taps "back" button and my navigationController pops back. But when it should pop to the main viewController it fails to restore the view of my initial viewController.
Do you have any clue how to pop back to the first viewController in navigationViewController hierarchy if that view controller has containers with embedded view controllers in them?
Or should I consider changing the architecture of my storyboard?
The fact that the view controllers in a navigation controller have child view controllers is not important. Only worry about the top-level view controllers that are pushed onto the navigation controller's stack. And only push/pop top-level view controllers, not children.
If you are having problems, you are probably doing something wrong, and will need to post a screenshot of your storyboard, along with the code that shows how you manage your navigation controller stack.
If you want your initial view controller to contain the proper subviews, you either need to hide/show what you need to make it to look like you want in viewDidDisappear as the user moves on to a new view, or you need to set it when they come back in viewWillAppear.
However your view is set up when you leave is how it will show up when you come back unless you change it. For example, in your root view controller:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// hide your menu, clean up the view to prepare it for when user pops back
}
OR
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// if menu is showing, hide it. Clean up view before user sees it
}
I am writing an iPhone application using the storyboards for an initial mockup. The problem I have right now is switching view controllers.
I have a table view controller and another view controller. All I want to do is use a back button to go back to the original screen, and I can do that, except the data disappears. The storyboard that I have is shown below.
I have the Back button going back to the original navigation controller. I have also had it going back to the Card view controller.
I have hard coded some example cells to just see how things look and they show up just fine when I run the simulation. When I click the back button though, it goes back to the All Cards screen and the cells that were there are now gone.
If I need to post some code just ask for what part would be helpful, I have done all of this through storyboards though.
I'm sure it's something stupid I've done, any point in the right direction would be greatly appreciated.
Basically: you pushed where you should have popped.
What you are seeing on the Storyboard does not exist yet. By segue-waying during runtime to a view controller it gets instantiated.
When you segue-wayed during runtime from the Add Card view controller "back" to the Card View Controller - here is what happened: instead of popping the navigation stack all the way back to the Card View Controller you already had, you just instantiated a new Card View Controller and pushed it onto the navigation stack. You could verify that by going all the way back to the original Card View Controller by tapping the back button several times.
What you could do to accomplish your task is this:
Instead of using the Storyboard for your back button use an IBAction in code:
- (IBAction)popToRoot:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
What I'm trying to do
If started creating my App. I've got a NavigationController as my rootViewController. When the app is started it will open my LoginViewController. In there I do the Login-Procedure, if everything is fine. I'll start my ECSlidingViewController. Which has a Menu (UITableView) and a MainView (UITableView).
Until here everything is working fine. I can swipe and it show's my menu. Now the problem is starting. When I press a menu-item it perfectly starts my new ViewController and show's the content. But there it still show's the same NavigationBar - like in the ECSlidingViewController. So I got no possibility to tell the user what he is looking at, also I need to show him sometimes new options in the NavigationBar.
Question
How can I fix this problem? I'd always like to show the NavigationBar of the specific ViewController I'm actually using. If you guy's have some Codesnippets or a good Idea how to handle this problem. Please leave a comment bellow.
If you need codesnippets or something else, tell me I'll upload it! Thank you in advance!
Code
This is how I start my ECSlidingMenu:
[self performSegueWithIdentifier:#"loginPush" sender:self.view];
This is how I'll start a new ViewController:
UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
Pictures*
You want to change your structure so that the ECSlidingViewViewController is the root view controller. This can either be from the start of the app or you can switch the root after login. The top view controller of the sliding controller should then be a navigation controller and you should set the root and push other view controllers into that navigation controller.
What you currently have breaks the link between the view controllers and the navigation controller because only the sliding view controller is pushed into the stack and its title (nothing about its navigationItem ever changes).
Probably the easiest solution would be to change the initial view controller in the storyboard (to the sliding view controller). In this case the login view would be presented as the root view of the navigation controller (which would be the top view). Then, after login you push the next view controller and then (after the animation completes) remove the login view controller from the nav stack.
in the top view of my view controller (the last table view controller) has an add navigation item. i added a view controller object from the objects library and i ctrl + dragged from the plus button to the view controller. i tried the app and it works fine but i can't go back to the previous controller when i reach the last controller. since the last controller connected (by segue) to the plus button, i can't have a navigation bar on top. so i added one and added an navigation item called it Done. i created an IBAction method in the class that the last controller subclasses which have the following code:
[self.navigationController popNavigationControllerAnimated:YES];
However, when i run the app and press the Done button to go back, it doesn't work although i feel like what i did is totally legal.
If you would have made the final view controller segue a Push segue, you'd still have the navigation bar with a back button. It makes sense since you're adding a record that you'd want a modal view.
You can dismiss the current modal view with the following code:
[self dismissViewControllerAnimated:YES completion:nil];
Generally, you should use delegation and dismiss it from the presenting view controller. However, I think it's fine to dismiss yourself if you're using storyboards, segues, and ARC.
Did you create a Bar Button Item and assigned its 'selector' callback?