Pushing view controller onto view control in tab controller - ios

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.)

Related

Call master view controller from within detail view of split view controller

I have a tab bar controller with 3 tabs, each embedded within its own navigation controller. This works great, but now I’m trying to put the entire tab bar controller within a split view controller.
The problem is the navigation on the master view for the split view controllers hides the existing navigation for the views inside the tab bar controller. If I hide the master view navigation, I get my tab bar controller navigation back but then I have no way of getting back to the master list. I can add the button back in programmatically but it only works on the iPad.
navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
navigationItem.leftItemsSupplementBackButton = true
I also tried playing around with the various options for preferredDisplayMode, but again, this only works on the iPad. It has no effect on the iPhone. I can add in a custom button programmatically to the view controller within the tab view controller and call an action from there, but I don't know what action to call. I tried calling the action on the displayModeButtonItem:
splitViewController?.displayModeButtonItem().action
That didn't work either. I would like to be able to just programmatically add back in the behavior of the back button that the split view controller adds on the iPhone.
I ended up scrapping the split view controller and just using anther tab. However I had a similar problem in another app and I figured out a kind of hacky solution. I embedded the tab bar controller itself inside a navigation controller. This causes multiple navigation controllers to appear so I did a check in viewWillAppear to hide whichever navigation controller is causing the issue.
if let hidden = tabBarController?.navigationController?.navigationBarHidden {
if hidden == true {
tabBarController?.navigationController?.navigationBarHidden = false
}
}
This app has a split view controller as the root view controller and then tab bar controllers in both the master and detail views. Every view is embedded in a navigation controller and I just show or hide the navigation bar depending on if I have a duplicate.

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)

Tab bar disappears when trying to go back from new view

I am very new to Xcode and have encountered an issue with my app. I am trying to create a tab bar app. On one of the tabs I have a button that brings the user to a different ViewController. I want to have it so the user can select a button that would return them to the tab that had the button. I tried to set up an action from the button to the previous view (the tabbed screen), however the tab bar disappears. I hope this is makes sense.
Here is a link to a screenshot...
Easiest way to do this is to place a UINavigationController as the root view controller of the TabBarController. You can do this in storyboard by simply ctrl+dragging from the tabbar controller to the navigation controller and adding it as a relationship.
Here's an example using storyboards:
The next step is to set the third controller (in this case the table view controller) to your player view controller class.
Then, you can use the default back button and animation that comes with the navigation controller. If you prefer to hide the navigation bar at the top of the screen, then you can use your custom back button to call
[self.navigationController popViewControllerAnimated:YES];
You can also choose custom animations / segues, etc. but using a navigation controller to help you navigate screens is probably the simplest approach.

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