Back to Navigation Controller's first view in TabBar app - ios

I got a tabbar application.
There is only 2 tabs in the tabbar. First tab is a NavigationController, there are two views in the NavigationController - root and detail views, second is a TableViewController. Second works perfect, but the the first doesn't.
I start in first view (NavigationController). Then go in this NavigationView to the detail view. Then, for example, i go to the second tabbar view. Then go back to the first, but i dont see the root view, but the detail view.
How can i open the root view every time?

Use the UITabBarController delegate method tabBarController:didSelectViewController: like this:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (viewController != tabBarItemForNavControllerTab) {
[self.navControllerInFirstTab popToRootViewControllerAnimated:NO];
}
}
Also make sure that delegate is setup properly, when creating your UITabBarController and you change the code from my snippet to fit your controller names.

Related

Setting a property of a View Controller embedded in Navigation Controller via Tab Bar

My app contains a Tab Bar Controller at the root which has to three tabs - each of which are View Controllers embedded in their own Navigation Controllers. Like this: Tab Controller -> Nav Controller -> View Controller.
I'm trying to set a property of the second tab's view controller from the first tab like this (as I would when preparing for segue)
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
if (tabBarController.selectedIndex == 1) {
((CreateViewController *)viewController).myProperty = YES;
}
The method is being called as expected, but as soon as I attempt to set the property the app crashes and I receive the error:
-[UINavigationController setAppearedFromTabBar:]: unrecognized selector sent to instance 0x7fc913d5f510
I have a feeling this has to do with the fact that the View Controller (CreateViewController *) is embedded in a Nav Controller. Do I need to pass the info to the Nav Controller which in turn will pass it to the View Controller? Any help would be appreciated!
Clearly you have reference to navigationController instead of CreateViwController.
((CreateViewController *)[(UINavigationController *)viewController topViewController]).myProperty = YES;

Navigation Controller between view controller and table view controller

Any ideas on how to add a Navigation Controller in iOS7 between a Table View and View Controller?
As soon as I embed the Navigation Controller to the table view the app crashes.
Without the Navigation Controller it works fine. But then the Table View becomes a View Controller. This is the code I'm using:
[self.navigationController performSegueWithIdentifier:#"setup" sender:self];
Here is my storyboard: http://sv.tinypic.com/r/1ztdnb/8
Okay, giving the comments here is what I think you want:
A LoginViewController which is a subclass of a UITableViewController. Should not display navigationBar.
A ProfileViewController also a subclass of a UITableViewController. Should display a navigationBar.
When a user taps LogIn button, ProfileViewController should be "pushed".
So the whole trick is to make navigation bar hidden for one view controller and visible for another. Well this is very easy to achieve - just use setNavigationBarHidden:animated: in viewWillAppear: for each view controller
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES/NO animated:animated];
}

Reload a tab after dismissing a view

There are two views in a tabbarviewcontroller. And the first tab has its view controller called myViewController which contain its IBOutlet. Pressing the button on the first view will present a view controller. After dismissing the view and back to the tab, the viewWillAppear of myViewController won't be called,but viewWillAppear of tabbarviewcontroller will. I need to reload the information on the first tab. If I use viewWillAppear in tabbarviewcontraller, how do I change the values of these property in myViewController? If anyone has idea ? Thanks.
If you want to call viewWillAppear of the viewController, you can add this to viewWillAppear of the TabBar
for (UIViewController *viewController in tabBarController.viewControllers)
{
[viewController viewWillAppear:YES]
}

How do i reset my tab to default view?

I have UITabViewController running with UINavigationController. In each tab i have a different TableViews that are roots of my application tree.
When i click an item of table, it goes to next level viewing another, detailed TableView, still having TabBar and NavigationBar on screen. It works perfectly, except for one thing. If i'm viewing details in one tab, then switch to another tab, and go back again, then i still see my detail. What i want to achieve, is to reset tab after leaving it.
I expect that i have to put something in viewDidUnload or simmilar, but couldn't find the right solution.
Hope you can help.
at
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
do
[viewController.navigationController popToRootViewControllerAnimated:YES];
That will navigate the tab's view controller to the root view whenever the tab is selected.
That's completely OK to keep your main window view controller (tab bar controller) instance at application delegate.
What you need to add is to set the delegate or whatever other initialized class to be the tab bar controller's delegate like this:
myTabBarController = [UITabBarController alloc ...
myTabBarController.delegate = self; // the app delegate will be also the tab bar delegate
in the app delegate, then you add the following method to the app delegate:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
[viewController.navigationController popToRootViewControllerAnimated:YES];
}
The tab bar controller will call this method whenever it's tab is selected.
You will also want to make the application delegate confirm UITabBarControllerDelegate this way:
#interface PSAppDelegate : UIResponder <UITabBarControllerDelegate>
that will let the compiler know that app delegate must or might have the methods declared in the protocol and will also give you so convenient auto-complition of this method.
One way to do this is to get the view controllers associated with the tabviewcontroller and then create new objects.
self.tabBarController?.viewControllers! will get an array of viewControllers for the tab bar. In my case they are a UINavigationControllers but they could be anything. I then get the viewController associated with that and reset that.
If you don't have navigation controllers you can just create new view controller objects and go with that.
below is the solution in my case (with the navcontroller)
let vc = self.tabBarController?.viewControllers![0] as! UINavigationController
let newVC = YourViewControllerClass()
vc.viewControllers[0] = newVC as UIViewController
This replaces the old (populated) vc with a new one!

Black view instead of table

I got a tabbar application.
There is only 2 tabs in the tabbar. First tab is a NavigationController, second is a TableViewController. Second works perfect, but the the first doesn't.
When i start application, i saw black window. And there is not title on the NavigationBar.
self.navigationBar.topItem.title = #"Routes";
What's the problem? Thnx.
Well, you haven't pushed a view controller to your navigation controller yet. A navigation controller is just a container for another UIViewController.
Do something like this:
- (void)viewDidLoad {
[super viewDidLoad]
// Initialize an UIViewController here ...
[self pushViewController:myRootViewController animated:NO];
}

Resources