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
}
}