Objective c 如何实现didSelectViewController

Objective c 如何实现didSelectViewController,objective-c,uitabbarcontroller,Objective C,Uitabbarcontroller,我想捕捉有人在选项卡之间切换时发生的事件。我的appdelegate文件中有以下两个函数: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UITabBarController * uitbc = [storyboard instantiateViewControllerWithIdentifier:@"tabbar

我想捕捉有人在选项卡之间切换时发生的事件。我的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(@“切换”)永不开火。xcode为行
uitbc.delegate=self发出警告显示“将appdelegate const_____;strong传递给不兼容类型id的参数”

我做错了什么?除了实例化我的tabbarcontroller表单故事板之外,我只是遵循这里找到的公认答案:

更新 根据skram的建议,我为我的appdelegate写了这篇文章,但NSLOG(切换)仍然没有启动:

@interface johnAppDelegate : UIResponder <UITabBarControllerDelegate>

好消息是没有崩溃。我也不再发出关于不兼容类型的警告。但是,didSelectViewController仍然不会启动。

您需要您的AppDelegate符合
UITabbarController远程门协议。见下文

@interface YourAppDelegate : UIResponder <UITabBarControllerDelegate>
@接口YourAppDelegate:UIResponder

在我的appdelegate.h文件中,我更改了行

@interface wscAppDelegate : UIResponder <UIApplicationDelegate>
然后在appdelegate.m文件中,我添加了这个函数

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

NSLog(@"hooray this works");

}

我也为此做了很多努力,但我的UITabBarNavigationController离AppDelegate非常远,无法将其设置在那里,而真正使其工作的唯一方法是在UITabBarController子类本身的viewDidLoad中执行,而不是在其:

-(void)viewDidLayoutSubviews {
    [ super viewDidLayoutSubviews ];
    self.delegate = self;
}

这就解决了问题。或者,您也可以在任何选项卡栏控制的ViewController中执行此操作,但这感觉不对。

仍然没有触发。我更新了我的问题以反映这一点。还有其他想法吗?谢谢你还收到警告吗?警告已经不见了,很好。也许我还没有在我的应用程序代理和正确的tabbarcontroller之间建立一些关系?
wscAppDelegate *appDelegate = (wscAppDelegate *)[[UIApplication sharedApplication] delegate];
self.delegate = appDelegate;
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

NSLog(@"hooray this works");

}
-(void)viewDidLayoutSubviews {
    [ super viewDidLayoutSubviews ];
    self.delegate = self;
}