I have two tabs in the application.
From tab 2 on a particular view i want to switch to Root View of the 1st tab.
how do i do this?
In this case simply self.tabBarController.selectedIndex = 0; wont work as it does not shows root view it shows the view to which user has navigated to before leaving that tab.
I wish to directly switch to first view of the tab 1
I'm guessing that tab1 is a UINavigationController. You will need to popToRootViewController on the first tab
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
Depending on how your application is configured, you might create a method on your appdelegate or somewhere in your view hierarchy that hosts both the UITabBarController and the UINavigationController.
If you always want to display the first view of a particular tab when the user returns to it, call the popToRootView from a viewDidDissapear method somewhere. There are lots of ways to do it, but think about the user experience if you do it automatically.
Related
I have a storyboard with tab bar controller and one of the tabs segue to another view controller and so on as show in the picture.
I want to go to the page (3) programmatically in the stack while maintaining the stack of the tab bar controller .
Thanks in advance..enter image description here
This one's a little tricky, maybe someone else here knows a better way of doing it but here's how I would tackle the problem. Let's assume for simplicity that all views are loaded already. Let me know if this works
Setup an observer in the view controller that is being displayed by the tab selected in the tab bar controller. (Let's call this page TabPageVC). When the event that the TabPageVC is observing is fired have it segue way to page 3 immediately
In app delegate when the app becomes active / enters foreground check to see if you need to display page 3. If you do need to then get the root view controller in app delegate (i'm assuming it's the tab bar view controller, if it's not you'll need to set it to be).
Set the selected tab in the tab bar controller to be the tab of the index that TabPageVC lives in
Trigger that event that TabBarVC is observing this will cause TabBarVC to immediately segue way to page 3 and you'll have retained the stack
I am answering my own question in this post.
Requirement: I want to have a tab view controller as the top parent. This will have 5 tabs. In the first tab, I want to have a segment control at the top which I want to switch the views within this first tab with information based off of which segment index is selected.
Reading a few suggestions online were to-
Either use containment view controllers where the first tab holds strong references to the child view controllers and switches them based off of which index is selected in the segment control. Problem with this is that each of the view controller will be eating up memory as we are keeping them in strong reference.
Second idea I read online was to put everything on a single view controller in the first tab - based on which index segment control is selected, just hide everything else. Problem with this was super messy code with too much stuff on the same view controller plus storyboard would get messy with things on top of each other.
Solution I came up was to embed another tabbarcontroller inside the first tab's view controller. Hide this second tab bar. This second tab bar will have the 3 child view controllers - each for each segmentcontrol's index. Whenever the segment is changed, I change the tab.
So in the second tabbarcontroller (self is the second tabbarcontroller)
-(void)segmentChanged:(UISegmentedControl*)sender{
NSLog(#"New value: %d",sender.selectedSegmentIndex);
[self setSelectedIndex:sender.selectedSegmentIndex];
}
This way iOS will put the view controllers in memory only when the segments are switched and not from the very beginning. Also iOS UIKit will handle the memory management for the tabs as mentioned here.
Plus we don't have to deal with messy code and storyboard shenanigans. Each segment control's index logic is separate in it's own view controller from the second tab.
Let's say I have 4 items in my UITabBar: A B C D.
Via the delegate methods, D pushes a new viewController from the UITabBar's navigationController, (removing the tabBar for that one view).
This works fine, but how can I keep the UITabBar from showing a blank view for D when going back from the new view?
I tried setting the selectedIndex to the previous index on push, but that just hangs the app (seems to work fine for modals, just not when pushing.)
If I interpret this correctly, when you're pushing a view as a result of selecting a tab, you're simultaneously changing the tab that's selected. This is probably trying to then change the view hierarchy that you're currently pushing a view on (that would be the result of selecting the other tab that you're trying to set it to). So it's probably crashing because it's replacing a view that's in the process of being displayed.
Also, from the sound of it you're using a UITabBarController inside of a UINavigationController. Apple says that you're not supposed to place a UITabBarController inside another view controller. The following is taken from Apple's docs on UITabBarController:
When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.
If this is the case, you should redesign your app so that you're not containing the tab bar controller in any other view controllers, or your app may behave oddly or stop working at some point.
If you wanted to make things behave exactly how you want them to, you could use a UITabBar directly, and implement your own UITabBarDelegate.
My app is based around a UINavigationController. On some screens I have a UITabBarController embedded within. When I go to a screen with the tab bar everything works fine on the first tab. (each tab is a UITableViewController) I can tap a cell of the table view and it'll take me to the next page by correctly pushing it onto my nav controller. If I go to another tab it loads the table view fine, but if I tap a cell to take me to another view I get this error:
Nested push animation can result in corrupted navigation bar
Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
It pushes on the next page, but then if I attempt to press the back button on the nav bar the app will crash.
How could I go about fixing this? I looked at multiple other responses to similar problems, but none of them have helped me with this.
It would be really helpful if you share your code.
However this may be helpful for you:
while using tab bar , you should try that every single tab has its own navigation controller and use individual navigation controller to move to specific viewcontrller of specific tab. I know its hard to understand like this..
For e.g in app del you have your main navigation controller using which you push to next view controller ,say "SecondViewController".
Now this second one has got one tab bar with 4 tabs tab1,tab2,tab3,tab4. now each tab can have any no of view controllers associated with them. For e.g on tab1,you move to another screen and again from there to next screen and so on.
So to manage them there should be separate navigation controllers like,tab1nav,tab2nav,tab3nav,tab4nav.
tabBarController.viewControllers = [NSArray arrayWithObjects:"tab1nav".....and so on all nav, nil];
Now depending in which tab you are, use that tab's navigation controller to move to next screen of that tab or move back.
If you have viewDidAppear function,
Remember to put [super viewDidAppear:animated]; inside the function.
In building my application which is a tab based application, from the first tab, the user has the option to view their profile information (which is specific to the app). SO have set up UINavigationController with following view controllers:
1 - Edit profile
0 - View profile (also the root view controller for the `UINavigationController`).
The flow I would like to achieve is if a profile has not been set up (i.e. the first time the application is run), I would like to go directly the Edit Profile View, which right now is the default behaviour since that view is at the top of the stack.
The problem I have run into is, if the profile has been set up, how would I go directly to View Profile. I have looked at the documentation for the UINavgationController, and it unclear about popping a view controller off the stack. The method popToViewController:animated return an NSArray of items popped from the stack. Does that mean those view controllers are no longer available, and/or is there a better method to go directly to the view controller that I want?
If you only have two views in the navigation controller, and View is the root view controller, you can make sure that View is the one that is shown by running popToRootViewControllerAnimated: immediately before or after that tab has been selected on the tab bar controller.
If you are in a case where you want Edit to show, run popToRootViewControllerAnimated: followed by pushViewController:animated: with the Edit view controller.
When you want to pop, you can use popViewControllerAnimated: rather than popToViewController:animated. (You only have two view controllers in this nav controller, so ther is only one that will ever be popped.)
This seems pretty simple, unless I have misunderstood your question.
The array of view controllers that are returned from the popToViewController:animated aren't needed by most programs. I haven't really found a need to use this method myself, and, as I said, it doesn't look like you need it here.