誰かがタブを切り替えたときにイベントをキャッチしたい。appdelegate ファイルに次の 2 つの関数があります。
- (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 は、 「互換性のない型 ID のパラメーターに appdelegate const__strong を渡す」という行に対して警告を発行します。
私は何を間違っていますか?tabbarcontrollerフォームストーリーボードをインスタンス化していることを除いて、ここで見つかった受け入れられた回答に従っています:
iPhoneでタブメニューを切り替えるイベントを取得する方法
更新 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 は起動しません。