简体   繁体   中英

Push multiple view controllers in storyboard

I have three controllers (FriendVC, ChatVC, PrivateChatVC) inside storyboad, and navigation is sequential :

A user can navigate from FriendVC to ChatVC (in TabBarController ), and then to PrivateChatVC .

Now, I need to make some button that will open PrivateChatVC from FriendVC but will also put ChatVC on navigation stack, so when a user will press back from PrivateChatVC he will be returned to ChatVC .

Problem I'm facing is that my ChatVC is a TabBarController .

Below is the code I'm trying:

   [self.tabBarController setSelectedIndex:1];
   PrivateChatController * privateChatController = [self.storyboard instantiateViewControllerWithIdentifier:@"privatechat"];
   [self.navigationController pushViewController:privateChatController animated:YES]; 

I'm assuming that [self.tabBarController setSelectedIndex:1] will load the tabbar and then [self.navigationController pushViewController:privateChatController animated:YES]; will load PrivateChatVC .

However, it only takes me to ChatVC and PrivateChatVC never loads.

You're on the right track, you need to select the ChatVC tab as you've done, but use a singleton class and set a param so that when ChatVC becomes the active VC it knows to immediately push PrivateChatVC.

In FriendVC:

_singleton.showPrivateChat = YES;
[self.tabBarController setSelectedIndex:1]; // select ChatVC tab

In ChatVC:

- (void) viewDidAppear {

    if (_singleton.showPrivateChat) {
        PrivateChatController * privateChatController = [self.storyboard instantiateViewControllerWithIdentifier:@"privatechat"];
        [self.navigationController pushViewController:privateChatController animated:YES]; 
        _singleton.showPrivateChat = NO; // reset boolean
    }
}

Warning!! This is very bad code! Use it at your own risk :)

ChatVC *chatVC = [self.storyboard instantiateViewControllerWithIdentifier:@"chat"];
PrivateChatController * privateChatController = [self.storyboard instantiateViewControllerWithIdentifier:@"privatechat"];  
[self.navigationController pushViewController:privateChatController animated:YES];
NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy];
[viewControllers insertObject:chatVC atIndex:viewControllers.count - 2];
self.navigationController.viewControllers = viewControllers;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM