didSelectViewController does not get called on certain occasions

后端 未结 3 1755
灰色年华
灰色年华 2021-01-21 20:21

I have the problem that many already have reported, didSelectViewController doesn\'t get called, but in my case it sometimes gets called. I have three tabs and thre

相关标签:
3条回答
  • 2021-01-21 20:32

    I too had this problem and got fed up with it. I decided to subclass UITabBarController and override the following methods. The reason I did both was for some reason on application launch setSelectedViewController: wasn't being called.

    - (void)setSelectedIndex:(NSUInteger)selectedIndex
    {
        [super setSelectedIndex:selectedIndex];
        // my code
    }
    
    - (void)setSelectedViewController:(UIViewController *)selectedViewController
    {
        [super setSelectedViewController:selectedViewController];
        // my code
    }
    
    0 讨论(0)
  • 2021-01-21 20:47

    I just dug through this tutorial on storyboards, and I thought of an alternative to using UITabBarControllerDelegate. If you want to stick to UITabBarControllerDelegate then feel free to ignore this answer.

    First, create a subclass of UITabBarController, let's call it MyTabBarController. In the storyboard editor you need to change the "Class" property of the tab bar controller so that the storyboard picks up your new class.

    Add this code to MyTabBarController.m

    - (void) prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
    {
      if ([segue.identifier isEqualToString:@"SecondVC"])
      {
        SecondViewController* secondViewController = (SecondViewController*)segue.destinationViewController;
        [secondViewController doSomething];
      }
      else if ([segue.identifier isEqualToString:@"ThirdVC"])
      {
        ThirdViewController* thirdViewController = (ThirdViewController*)segue.destinationViewController;
        [thirdViewController doSomethingElse];
      }
    }
    

    In the storyboard editor, you can now select the two segues that connect to SecondViewController and ThirdViewController and change the segue identifier to "SecondVC" and "ThirdVC", respectively.

    If I am not mistaken, that's all you need to do.

    0 讨论(0)
  • 2021-01-21 20:58

    It's hard to follow what exactly you are doing, but from what I understand you are responding to tab switches by changing the UITabBarController's delegate back and forth between SecondViewController and ThirdViewController.

    If that is true, I would advise against doing this. Instead I would suggest you try the following:

    • Assign a delegate that never changes. For a start you could use your app delegate, but it would probably be better if you had a dedicated small class for this. I am sure that now you have a non-changing delegate, it will get 100% of all the calls to tabBarController: didSelectViewController:.
    • The object that is the delegate must have a reference to both the SecondViewController and ThirdViewController instances. If you are designing your UI with Interface Builder, you might do this by adding two IBOutlets to the delegate class and connecting the appropriate instances to the outlets.
    • Now when the delegate receives tabBarController: didSelectViewController: it can simply forward the notification to either SecondViewController or ThirdViewController, depending on which of the tabs was selected.

    A basic code example:

    // TabBarControllerDelegate.h file
    @interface TabBarControllerDelegate : NSObject <UITabBarControllerDelegate>
    {
    }
    
    @property(nonatomic, retain) IBOutlet SecondViewController* secondViewController;
    @property(nonatomic, retain) IBOutlet ThirdViewController* thirdViewController;
    
    
    // TabBarControllerDelegate.m file
    - (void) tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController
    {
        if (viewController == self.secondViewController)
          [self.secondViewController doSomething];
        else if (viewController == self.thirdViewController)
          [self.thirdViewController doSomethingElse];
    }
    

    EDIT

    Some hints on how to integrate the example code from above into your project:

    • Add an instance of TabBarControllerDelegate to the .xib file that also contains the TabBarController
    • Connect the delegate outlet of TabBarController' to the TabBarControllerDelegate instance
    • Connect the secondViewController outlet of TabBarControllerDelegate to the SecondViewController instance
    • Connect the thirdViewController outlet of TabBarControllerDelegate to the ThirdViewController instance
    • Add a method - (void) doSomething to SecondViewController
    • Add a method - (void) doSomethingElse to ThirdViewController
    • Make sure that you don't have any code left in SecondViewController and ThirdViewController changes the TabBarController delegate!

    Once you are all set and everything is working fine, you will probably want to cleanup a bit:

    • Change the names of the notification methods doSomething and doSomethingElse to something more sensible
    • If you followed the discussion in the comments, maybe you also want to get rid of the secondViewController and thirdViewController outlets
    0 讨论(0)
提交回复
热议问题