UINavigationController missing from view hierarchy after cancelling UISearchController - ios

I have an app with a UITabBarController and a few tabs, each of which contains a UINavigationController. I also have a UISearchController triggered by a search button on the nav bar, which has a custom search results view controller. Search works just fine, as does tapping cancel. I implemented presentSearchController: to present the search controller as a view controller using
[self presentViewController:animated:completion:];
, which also works fine.
Where I run into trouble is if the user dismisses the keyboard (search controller active), and then switches tabs on the tab bar, comes back to the original tab with search controller active, and then taps "Cancel", the underlying UINavigationController's view stack is gone from the main window hierarchy and doesn't get reloaded until I switch tabs and come back with search mode inactive.
This is similar to this issue: "From View Controller" disappears using UIViewControllerContextTransitioning
Except I don't use any custom transitions, and the tab bar still shows after tapping cancel. Printing the view stack shows the tab bar is the only subview of the window until I switch tabs again and everything displays as normal.
What is the best way to go about solving this? I really hate to brute force re-add the navigation controller's view to the window on didDismissSearchController: as suggested. Not only does this seem like a bad idea, but I also run into issues with the z-ordering of the tab bar and the navigation controller when explicitly re-adding the nav controller to the key window.

Adding my brute-force solution:
- (void)didDismissSearchController:(UISearchController*)searchController
{
if (![self.view isDescendantOfView:[UIApplication sharedApplication].keyWindow]) {
NSUInteger currIndex = self.tabBarController.selectedIndex;
NSUInteger tempIndex = self.tabBarController.selectedIndex == 0 ? 1 : 0;
[self.tabBarController setSelectedIndex:tempIndex];
[self.tabBarController setSelectedIndex:currIndex];
}
}

Related

Pushing view controller onto view control in tab controller

So I have a tab bar controller that holds a search view and a profile view. When I click on one of the cells in the search view I want to go to another view controller, still have my tab s on the bottom and maintain the user's ability to click a back button to go back to the main view.
I've achieved the back button part, but I haven't achieved the maintaing tabs part.
This is what I've tried -
-(void)displayCardController{
if(self.userProfile == nil){
[self.tabBarController setViewControllers:#[self.searchViewController, self.loginViewController]];
[self.searchViewController.navigationController pushViewController:self.searchViewController.detailController animated:YES];
} else {
[self.tabBarController setViewControllers:#[self.searchViewController, self.profileViewController]];
[self.searchViewController.navigationController pushViewController:self.searchViewController.detailController animated:YES];
}
}
The idea is - set the tab to have my controllers, and then push what I want to be on top. That doesn't work.
How do I achieve this?
It looks like the problem is that your first tab controller child, self.searchViewController, has a navigation controller. If you want to be able to push onto this controller while still staying inside the tab controller, you need the search view controller (or whatever is the first tab controller child) to be a navigation controller.
Note that its navigation bar can be hidden, so it won't look like a navigation controller, but when you push, you can show the nav bar and so give the user a clear way to get back.
Alternatively, use a different interface. What I do, for example, when I have two tabs and one of them needs to change temporarily, on the iPhone, is use a presented view controller: instead of push/pop, I use use present/dismiss. On the iPhone, this hides the tab bar, but we return to the same place when we're done so the interface is clear. (On the iPad, a presented view inside a tab bar controller does not have to hide the tab bar.)

Corrupted UINavigationController with embedded UITabBarController

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.

How do you change views from a Tab Bar Controller

I am developing an app that consists of a Tab Bar Controller that points to 3 view controllers (all with tabs). In one of these tab views I've made a button and I want it to open a new view (without a tab at the bottom). This new view would need a navigation bar with a back button to return to the previous view, so I was thinking I need to create a navigation controller?
Essentially this is what I'm trying to do (I apologize for the poorly drawn diagram).
How can I get this new view (entirely independent of the tab bar controller) to display programatically? Would this require a navigation controller?
You are describing a presented view controller. Call presentViewController:animated:completion:.
I very frequently do this with a navigation bar and a Back or Done button, just as you describe. But it's not a navigation controller or navigation interface; it's just a convenient way of showing the user how to get back.
For example, this is a presented view in one of my apps. The top is a navigation bar, and the cancel button gets us back (call dismissViewController...). The rest is a scrolling view (a UICollectionView) of buttons.
[myTabBar setSelectedIndex:1]
You may have to access the tabBar like self.tabBarController so… [self.tabBarController setSelectedIndex:1];
1 is index 1 in the tabbar's stack (this is like tapping a tabBar button manually)

Problem with "Presenting a controller modally within a nav controller within a tab bar controller"

My app has two distincts modes. There's a tab bar controller in the app delegate. There are two tabs, both using subclassed view controllers. The two view controllers essentially contain a nav controller each. The nav controllers have their root view controller, and normally when changing screens, I just push and pop controllers of the respective nav controller. This has the (normal) effect that the bottom tab bar is always visible, all great and sound.
This one time I'd like to present a screen modally however, so that the user can't do anything else than confirm or cancel the page using two buttons, ie I want to hide also the bottom tab bar. This would be a case for presenting the view modally I thought, but the view is presented within the nav controller bounds it seems, so the bottom tab bar is still visible, and this causes confusion in navigation the app. I'm not sure how it's possible that the modally presented view is not hiding the tab bar. Most of the questions around here seem to have the problem the other way around (wanting to (incorrectly) present a modal view and leave the tab bar visible).
These are my attempts:
[self presentModalViewController:controller animated:YES]; // inside tab bar controller :-(
[self.tabBarController presentModalViewController:controller animated:YES]; // nothing is displayed. The new controller is instantly deallocated.
[self.navigationController presentModalViewController:controller animated:YES]; // inside tab bar controller :-(
Investigating this, the self.tabBarController is actually nil. There seems to be no link back to the tab bar controller... I guess, to display modally on top of the tab bar, I need to get a link to that tab bar controller?
I seem to have found a solution, I'm not sure it's kosher, because somehow I wasn't able to use the self.tabBarController pointer of the view controller in which I start the view controller call.
What I did was reach for the app delegate, the app delegate having the tab bar controller defined as a public property. I could use that tab bar controller property to modally display my view controller over everything on the screen.

Hidden UINavigationController inside UITabBarController

I have an application with 5 UIViewControllers each inside a corresponding UINavigationController, all tucked inside a UITabBarController that displays 5 tabs at the bottom of the screen.
I want to display another UIViewController (inside a UINavigationController) when a dialog button is pressed.
This view should only be loaded and unloaded programatically; i.e. it should not appear in the tab bar. However, I want the tab bar to be visible always.
If I add the [UINavigationController view] to [self window] the UITabBar is covered. If I add it to any other layer, the UINavigationController adds on the compensation it has for the status bar so appears further down than expected.
A solution would be to have the 6th UINavigationController added to the UITabBar with the others, but with its tabBarItem hidden. Then I can show it and hide it using the tabBars selectedIndex property.
Accessing the tabBarItem through the UIViewController shows no obvious way of doing this.
#wisequark, I think you completely misunderstood and you have almost rewritten the architecture of my application. However I have a separate navigation controller for each view as they are mutually exclusive and there is no concept of "drilling down".
#Kendall, This is what I expect I will have to do - have the modal view appear with a hide button to bring back the normal interface. But it would be nice to keep the tab bar always visible, so I was just wondering if anyone knew of a way.
It sounds as though you have a mess on your hands. A UINavigationController is a distinct object that is very different from a UITabBarController. In general, your application should have a tab controller, one of who's tab's loads a UINavigationController which in turn loads it's views - not that both maintain management over the different views. It is also improper to refer to the display of a UIViewController as such an object doesn't have a visual representation. In the case of a UINavigationController, the navigation controller object is responsible for displaying a navigation bar and a table view (in the most common case) and for managing the display of all the views in the navigation hierarchy. It itself has no corresponding representation on screen. Similarly, a UITabBarController presents a tab bar and is responsible for the loading and unloading of the views and/or view controllers attached to the tab buttons. If we were to present this as an image, it would look something like this -
alt text http://img.skitch.com/20081112-2sqp7q4wafa34te1ga337u4k8.png
Well, it sounds like what you really want to do is present a modal view with the tab bar still visible. You could add your view as a subview of the tab bar controller's view. The tab bar's view is, oddly enough, not the tab bar itself but rather a view containing the tab bar and the selected item's view.
Alternatively, you could try calling presentModalViewController:animated: with the selected tab (i.e. [tabBarController.selectedViewController presentModalViewController:animated:]) as the receiver instead of the tab bar. I seem to recall doing this once (quite by accident) and the tab bar remained visible.
One more thought: since each of your five view controllers is a UINavigationController, you could always pushViewController:animated: onto the selected view controller, then hide the back button. Your view will just appear without animation. But you'll need to remember to pop your view controller off the stack whenever the user switches to another tab. That might take a bit more work.
The best idea I could think of would be to either push a modal navigation controller for your view (which would hide the tab bar which you do not want), or to get the tab bar controller current selected view controller (really your navigation controller for a tab) and push your new view controller on there - and then pop that view when another tab is selected with a tab bar delegate.
It seems wierd to me to push the view onto random tabs though, if the view is created from a dialog that is modal, I don't see why the view itself should not also be modal and hide tabs.

Resources