how to get the event that switch tab menu on iphone

倾然丶 夕夏残阳落幕 提交于 2019-11-26 21:08:48

问题


I'm trying to figure out how to catch the event that controls the switch tabs on the UITabBarController. How could I accomplish this?


回答1:


If you are using storyboard, do this

in didFinishLaunchingWithOptions

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
[tabBar setDelegate:self];

Also in AppDelegate, keep <UITabBarControllerDelegate>

And then

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
   //Write your code here
}



回答2:


Implement UITabBarControllerDelegate e.g. in your app delegate's applicationDidFinishLaunching

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    tabBarController.delegate = self;
    [window addSubview:tabBarController.view];
}

Then implement either:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;

The first method is called before the view switch and gives you a chance to 'veto' the view switch by returning NO

The second method is called after the view switch has taken place




回答3:


Have a look at the following method in UITabBarControllerDelegate:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

Tells the delegate that the user selected an item in the tab bar.




回答4:


Is UITabBarControllerDelegate what you're looking for, particularly -tabBarController:didSelectViewController:?




回答5:


Better late than never. In case of swift 4 you can do it in the following way.

tabBarViewController.delegate = self

And implement UITabBarDelegate in your class. You will get the callback in

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
  //Stuff to do
}


来源:https://stackoverflow.com/questions/2041982/how-to-get-the-event-that-switch-tab-menu-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!