0

appDelegateにUITabBarControllerがインスタンス化されていますが、ユーザーがさまざまなタブバー項目(タブバーのボタン)をいつ押したかを知る方法が必要です。

-UITabBarDelegateプロトコルを救助します(必要なdidSelectViewControllerメソッドを使用)!

Interface Builderにすべてが接続されている状態で、押されたこのタブバーアイテムに対応する実際のUIViewControllerサブクラスインスタンスへの参照を取得するにはどうすればよいですか?

タブバー項目が押されるたびにUIViewControllersサブクラスの1つでメソッドを呼び出す必要があるため、この参照が必要です。

助言がありますか?

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{   
    NSLog(@"%@", [[self.tabBarController selectedViewController] nibName]); // nil, no success here     

    if ([theTabBarController selectedIndex] == 1) {         
        MySecondViewController *reference = (MySecondViewController *) viewController;      

    if ([reference isKindOfClass:[UINavigationController class]]) {
        NSLog(@"OMG. It's a UINavigationController class??!"); // kicks in for some reason, shouldn't reference be a MySecondViewController
    }   
}
4

1 に答える 1

0

おそらく私はあなたの質問を正しく理解していませんが、あなたが求めているのは、あなたが言及したメソッド呼び出しに渡される単に「viewController」パラメーターであるようです

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

UITabBarController にも同じ情報を取得するプロパティがあります

@property(nonatomic, assign) UIViewController *selectedViewController
于 2010-09-23T02:07:23.180 に答える