0
votes

I have two frame in ios, let's say View1 and View2. View1 contains a table view. When you tap a cell you're being redirected to View2 with a detail of what you tapped. View1 is connected to a tabbar button, but if i tap the button after i went to View2, i'm returned to View2 and not View1, so i put inside View2 this code:

 -(void) viewWillDisappear:(BOOL)animated{
      [self.navigationController popViewControllerAnimated:NO];
 }

That is, when View2 disappear pop to View1, so in case i return back, using tabbar button i will be presented with View1

So far, so good.

Now, inside View2 there is a link presenting a MFMailComposeController so that you can send a mail. When i get back from mailing, the app crashes. It's totally right for viewWillDisappear is not distinguishing if View2 is disappearing for you're setting a mail or for you navigated to another View and the come back through tabbar. So, there is a way to distinguish if View2 is being presented for a tabbar button is being pressed or anything else happened?

1

1 Answers

1
votes

You can check self.tabBarController.selectedIndex inside viewDidDisappear. If the index is same as index of the view controller, then view controller need not be popped. When you switch tabs, the selectedIndex changes, so the view controller has to be popped.

-(void) viewDidDisappear:(BOOL)animated {
      if (self.tabBarController.selectedIndex != correctIndex )
      {
          [self.navigationController popViewControllerAnimated:NO];
      }
}