开发者

how to get the event that switch tab menu on iphone

开发者 https://www.devze.com 2022-12-16 13:44 出处:网络
I\'m trying to figure out how to catch the event that controls the switch tabs on the 开发者_运维知识库UITabBarController. How could I accomplish this? Implement UITabBarControllerDelegate e.g. in you

I'm trying to figure out how to catch the event that controls the switch tabs on the 开发者_运维知识库UITabBarController. How could I accomplish this?


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


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
}


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.


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
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消