How to implement didSelectViewController

后端 未结 3 1715
难免孤独
难免孤独 2020-12-10 20:50

I want to catch the event when someone switches between tabs. I have the following two function in my appdelegate file:

- (BOOL)application:(UIApplication *         


        
相关标签:
3条回答
  • 2020-12-10 21:01

    You need your AppDelegate to conform to UITabBarControllerDelegate protocol. See below..

    @interface YourAppDelegate : UIResponder <UITabBarControllerDelegate>
    
    0 讨论(0)
  • 2020-12-10 21:14

    in my appdelegate.h file, I changed the line

    @interface wscAppDelegate : UIResponder <UIApplicationDelegate>
    

    to

    @interface wscAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
    

    Then in my CustomTabBarController in the viewDidLoad function i added these lines:

    wscAppDelegate *appDelegate = (wscAppDelegate *)[[UIApplication sharedApplication] delegate];
    self.delegate = appDelegate;
    

    Then in appdelegate.m file, I added this function

    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
    {
    
    NSLog(@"hooray this works");
    
    }
    
    0 讨论(0)
  • 2020-12-10 21:14

    I also struggled with this a lot but my UITabBarNavigationController is pretty far from AppDelegate to set it there, and the only way to actually make it work was to do it not in the viewDidLoad of the UITabBarController subclass itself, but in its:

    -(void)viewDidLayoutSubviews {
        [ super viewDidLayoutSubviews ];
        self.delegate = self;
    }
    

    And that solved it. Alternatively you can do it in any of the tab bar controlled viewcontrollers, but that just feels wrong.

    0 讨论(0)
提交回复
热议问题