如何实现didSelectViewController [英] How to implement didSelectViewController
问题描述
我想在有人在选项卡之间切换时捕获事件.我的appdelegate文件中具有以下两个功能:
I want to catch the event when someone switches between tabs. I have the following two function in my appdelegate file:
- (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");
从不触发. xcode对行uitbc.delegate = self;
发出警告,说将appdelegate const__strong传递给不兼容类型id的参数".
But the NSLog(@"switching");
never fires. The xcode issues a warning for the line uitbc.delegate = self;
saying "Passing appdelegate const__strong to parameter of incompatible type id".
我做错了什么?除了要实例化tabbarcontroller表单故事板之外,我只是在这里找到公认的答案:
What am I doing wrong? I'm just following the accepted answer found here, except i'm instantiating my tabbarcontroller form story board:
更新 基于skram的建议,我为我的appdelegate编写了此代码,但NSLOG(Switching)仍然不触发:
Update Based on skram's suggestion, I wrote this for my appdelegate but the NSLOG(Switching) still doesn't fire:
@interface johnAppDelegate : UIResponder <UITabBarControllerDelegate>
我还更新了didFinishLauchingWithOptions:
I also updated my didFinishLauchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
tabBarController = self.window.rootViewController.tabBarController;
tabBarController.delegate = self;
[window addSubview:tabBarController.view];
}
好消息是没有崩溃.我也不再对不兼容类型发出警告.但仍然,didSelectViewController不会触发.
Good thing is that nothing crashes. I also no longer et the warning about incompatible types. But still, didSelectViewController doesn't fire.
推荐答案
在我的appdelegate.h文件中,我更改了该行
in my appdelegate.h file, I changed the line
@interface wscAppDelegate : UIResponder <UIApplicationDelegate>
到
@interface wscAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
然后在我的CustomTabBarController的viewDidLoad函数中,添加了以下几行:
Then in my CustomTabBarController in the viewDidLoad function i added these lines:
wscAppDelegate *appDelegate = (wscAppDelegate *)[[UIApplication sharedApplication] delegate];
self.delegate = appDelegate;
然后在appdelegate.m文件中,我添加了此功能
Then in appdelegate.m file, I added this function
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"hooray this works");
}
这篇关于如何实现didSelectViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!