[英]tabBarController:shouldSelectViewController: not being called when delegate is set
I think I've done my homework here. 我想我已经完成了我的作业。
I want my app delegate to be the delegate for my UITabBarController
. 我希望我的app委托成为我的
UITabBarController
的委托。
Using IB, I've connected UITabBarController
's delegate to my Application Delegate. 使用IB,我已将
UITabBarController
的委托连接到我的Application Delegate。
App Delegate Header file is: App Delegate Header文件是:
@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
The delegate method I'm trying to implement is: 我试图实现的委托方法是:
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSLog(@"shouldSelectViewController called.");
// do some stuff with viewController
return YES;
}
My app delegate has an outlet to the UITabBarController
that's connected in IB. 我的app委托有一个连接到IB的
UITabBarController
的插座。 When I do this: 当我这样做:
NSLog(@"tab bar controller delegate is %@", self.tabBarController.delegate);
I get a good result such as tab bar controller delegate is <MyAppDelegate: 0x6e86a30>
. 我得到了一个很好的结果,如
tab bar controller delegate is <MyAppDelegate: 0x6e86a30>
。
What am I missing? 我错过了什么?
Just write this code. 只需编写此代码即可。 Usually in
viewDidLoad()
. 通常在
viewDidLoad()
。
self.tabBarController.delegate = self;
If the current controller is a UITabBarController
then: 如果当前控制器是
UITabBarController
那么:
self.delegate = self
Ok, found the solution. 好的,找到了解决方案。 I had some old code in my
RootViewController
that set up this controller as the delegate. 我在
RootViewController
中有一些旧代码,它将此控制器设置为委托。 No delegate methods were implemented on the RootViewController
, so it appeared as if nothing was happening. RootViewController
上没有实现任何委托方法,所以看起来好像什么也没发生。 Because the RootViewController
is set as delegate AFTER MyAppDelegate
, the delegate was actually set to the RootViewController
. 因为
RootViewController
在MyAppDelegate
之后被设置为委托,所以委托实际上被设置为RootViewController
。
So the lesson is double-check your code to make sure some other object isn't also being set as the delegate. 所以课程是仔细检查你的代码,以确保其他一些对象也没有被设置为委托。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.