Before writing the chat program encountered this problem, we can in WeChat: from the contact interface to details page again to chat, in the chat interface is returned back to the message list the question has bothered me for a long time, it related to the structure of the project, the initial structure of our project is as follow:




So each of our VC’s had its own navigation, and after various experiments, it was not possible to jump from one VC of the tabbarController to another VC of the tabbarController in this case (mainly because it was hard to swipe right back), so I thought, Add another navigation to tabbarController, usually use VC’s own navigation to jump, and use tabbarController’s navigation to jump when jumping to the chat interface through the contact details page, so that the jump logic in wechat can be achieved, just like the structure below:




The downside of this approach is that we need to manage two navigations, and the jump animations are not the same as the native navigation jump animations when using the outer navigation for the jump. Method that is currently used in my project, at that time after using this method is also didn’t think much of that later met with a lot of pit, here also not much said Recently that I think we can actually VC navigation is removed, all use tabbarController navigation do jump, so it will be easier, and can avoid a lot of pit, It looks like this:




This kind of structure to realize I didn’t use in actual project, the main is a great trouble to my original project change, I am too lazy to change, I wrote here a Demo test, the effect is very good, so I write this to record, if you have better ways of implementation, can leave a message below, I am here to thank you

  1. Let’s create a UITabBarController subclass
    // Write TabBarController as a singleton class because we need to fetch it globally and set its check VC Static ViewController *shareInstance = nil; +(ViewController *)sharedInstance{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ if (shareInstance == nil) { shareInstance = [[ViewController alloc]init]; }}); return shareInstance; }Copy the code
  2. Note that the title of the VC navigation in the TabBarController has changed, and we need to set the title as follows
    // Set title -(void)viewWillAppear:(BOOL) Animated {[super viewWillAppear:animated]; TabBarController [ViewController sharedInstance]. Title = @" message "; }Copy the code
  3. Jump to chat interface code
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
     CCDetailViewController *DVC = [[CCDetailViewController alloc]init];
     [self.navigationController pushViewController:DVC animated:YES];
    }Copy the code
  4. In the chat screen
    -(void)viewDidAppear:(BOOL)animated{[super viewDidAppear:animated]; // let TabBarController select the message list interface [[ViewController sharedInstance] setSelectedIndex:0]; // Remove VC from navigation stack and add tabbarController and chat interface, When the right gestures so may also directly back to the message list interface [self. The navigationController setViewControllers: @ [[ViewController sharedInstance], self]]. } / / return button click event - (void) backClick: (UIButton *) BTN {[self. The navigationController popToRootViewControllerAnimated: YES]; }Copy the code

Attached is the address of Demo github.com/cdcyd/Commo…