简体   繁体   English

在UiTabBarViewController中实现shouldSelectViewController方法

[英]Implementing shouldSelectViewController method in UiTabBarViewController

I'm using a storyboard and have UITabBarViewController as my navigation controller. 我正在使用故事板并将UITabBarViewController作为我的导航控制器。 What I need is to use shouldSelectViewController method which is a part of a delegate UITabBarControllerDelegate. 我需要的是使用shouldSelectViewController方法,它是委托UITabBarControllerDelegate的一部分。

I'm not sure how to override this method in my CustomUiTabBarController . 我不知道如何在CustomUiTabBarController覆盖此方法。

I was trying to define interface like this: 我试图定义这样的界面:

@interface CustomTabBarController : UITabBarController <UITabBarControllerDelegate>

Then I implemented this: 然后我实现了这个:

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
//nothing yet
}

However shouldSelectViewController is not executed when I click on tabs. 但是,单击选项卡时,不会执行shouldSelectViewController。 I also can't set CustomTabBarController class as a delegate of a TabBar in my storyboard because it causes exception. 我也无法在我的故事板中将CustomTabBarController类设置为TabBar的委托,因为它会导致异常。

I clearly leak some iOS undesrtanding here. 我明显泄漏了一些不受欢迎的iOS。 Please assist. 请协助。

First of all , get the UITabbarController's object from the application's view controller flow. 首先,从应用程序的视图控制器流中获取UITabbarController的对象。 Then make a tabbarcontroller's property in appDelegate and assign it's value like, 然后在appDelegate中创建一个tabbarcontroller的属性,并为其分配值,如:

if([viewController isKindOfClass:[UITabBarController class]]){
            UITabBarController *objTabbarController = (UITabBarController *) viewController;
            self.objTabbarController = objTabbarController;
            self.objTabbarController.delegate = self;
        }

assign the delegate to it , 将代表分配给它,

@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarDelegate>

And implement the method in AppDelegate.m file 并在AppDelegate.m文件中实现该方法

In shouldSelectViewController just return YES; shouldSelectViewControllerreturn YES;

And In 并在

 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"tab selected index %d",tabBarController.selectedIndex);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM