How to go to “Home-page” in iOS App?

2019-03-03 22:30发布

问题:

I have UIViewController and UITabBarController. UIViewController contain 5 buttons and UITabBarController has 5 Tabs.

By tapping first button app shows TabBarController with the first selected tab, second - TabBarController with the second tab and so on...

To prepare this I have used Modal-segue for every button.

Everything works well.

Now I need to create "Home" button (may be programmatically) on TabBarController's UINavigationBar, which will execute "Go home view" action.

Edited with more details

{see the screenshot below}

  1. Initial UIViewController
  2. UITabBarController
  3. NavigationControllers
  4. UIViewControllers

There are TWO modal-segues between 1 and 2 (two buttons - two segues)

In my prepareForSegue in UIViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    AppDelegate *d = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if ([segue.identifier isEqualToString:@"s1"]) {
        d.initialTab = 0;
    }
    else if ([segue.identifier isEqualToString:@"s2"]) {
        d.initialTab = 1;
    }
}

In my viewDidLoad in UITabBarController:

TabController *mainTabController = (TabController *)self;
    AppDelegate *d = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [mainTabController setSelectedIndex:d.initialTab];

This is works!

But I need to go back to home-page. So if there is a NavigationController after TabBarController I would like to create a button in NavigationBar.

回答1:

I suggest you to take a one Navigation Controller as a parent.
Then, add your Startup View as a root view of it.

On your main view controller page,

- (void)viewWillAppear:(BOOL)animated
{
   self.navigationController.navigationBarHidden = NO;
}

So, it will hide your main navigation. So no need to worry for this internal navigation flow.
And for internal navigation, you have already taken different navigation controllers.

By using code:

[self.parentViewController.navigationController popToRootViewControllerAnimated:YES];

you'll be at root of the application and it will poped out all view controller which were stacked in application.

Hopefully, you'll understand flow & will apply if you feel good.

Enjoy Coding.
Thanks.