当有人在标签之间切换时,我想捕捉事件。我的 appdelegate 文件中有以下两个函数:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController * uitbc = [storyboard instantiateViewControllerWithIdentifier:@"tabbarcontroller"];
uitbc.delegate = self;
[self.window addSubview:uitbc.view];
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"switching");
}
但NSLog(@"switching");
永远不会开火。uitbc.delegate = self;
xcode 对“将 appdelegate const__strong 传递给不兼容类型 id 的参数”这一行发出警告。
我究竟做错了什么?我只是按照此处找到的公认答案,除了我正在实例化我的 tabbarcontroller 表单故事板:
更新 根据 skram 的建议,我为我的 appdelegate 写了这个,但 NSLOG(Switching) 仍然没有触发:
@interface johnAppDelegate : UIResponder <UITabBarControllerDelegate>
我还更新了我的 didFinishLauchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
tabBarController = self.window.rootViewController.tabBarController;
tabBarController.delegate = self;
[window addSubview:tabBarController.view];
}
好在没有崩溃。我也不再发出关于不兼容类型的警告。但是,didSelectViewController 仍然没有触发。