didSelectViewController方法不被调用(与故事板)
我有一个选项卡的ios5应用程序的2个版本,一个使用故事板和一个使用xib文件创build。 故事板版本不调用UITabBarControllerDelegate
方法didSelectViewController
(xib版本)。 故事板上有一些东西(我认为)缺失,但是我不知道是什么。 另一个框架问题的方法可能是 – 我怎样才能引用由故事板实例化的UITabBarController
对象?
谢谢你的帮助。
编辑:标签栏控制器委托设置:
在AppDelegate.h中:
@interface MyAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> @property (strong, nonatomic) UITabBarController *tabBarController;
在AppDelegate.m中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.tabBarController.delegate = self; return YES; }
然后在AppDelegate.m中,委托方法是:
- (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { NSLog(@"Got Here"); }
NSLog输出从不出现。 这个问题在我看来是,我没有正确引用由故事板实例化的标签栏控制器对象。 我怎么做?
我有这个问题。
如果你不使用故事板,那么在AppDelegate
设置UITabBarController
委托就行了。 但是,在Storyboards
, AppDelegate
并不知道tabBarController
在启动的位置。 你会想通过tabBarController
并添加委托方法:
(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { }
…就够了。 但是,这不是很刺激。
我需要知道用户何时按下了一个标签button。 我需要知道更多,我需要知道viewController的“ - (void)viewWillDisappear:(BOOL)animated {}
”方法已经运行。
我决定让我的UITabBarController
自己的代表。 这似乎很愚蠢,但我做了以下…
#import <UIKit/UIKit.h> @interface PlumbsTabBarController : UITabBarController <UITabBarControllerDelegate> @end
然后,在我的viewDidLoad
方法中,写了以下内容:
[self setDelegate:self];
这使我的标签栏委托方法运行。
疯狂还是什么?
好的 – 我现在正在编辑这个答案,因为即使上述都是正确的,在使用navigationController
的情况下,触摸每个tabBarButton
,selectdidSelectViewController
委托方法,当你尝试NSLog(@"%@", viewController);
只显示你已经select了UINavigationController
类?
所以,为了增加更多的复杂性,总的解决scheme是为每个你想要监视的viewController
的UINavigationController
子类,当tabBarbutton
被触摸时(做某事)。
无论如何,它适用于我。 而且,如果有人能够通过上述运球来挑剔,他们可能会发现一个有用的方面 – 这对我来说已经足够了,因为我觉得这个网站也是非常有用的。
把[self setDelegate:self];
在你的ViewDidLoad
或者对象被初始化的地方