我有 2 个版本的选项卡式 ios5 应用程序,一个使用 Storyboard创建,一个使用 xib 文件创建。 Storyboard版本不调用 UITabBarControllerDelegate
方法 didSelectViewController
(xib 版本调用)。 Storyboard 中(我认为)缺少某些东西,但我不知道是什么。另一种构建问题的方式可能是——我如何引用 Storyboard实例化的 UITabBarController
对象?
感谢您的帮助。
编辑:设置标签栏 Controller 委托(delegate):
在 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后面,委托(delegate)方法是:
- (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"Got Here");
}
NSLog 输出永远不会出现。在我看来,问题是我没有正确引用 Storyboard实例化的选项卡栏 Controller 对象。 我该怎么做?
最佳答案
我有这个问题。
如果您不使用 Storyboard,则可以在 AppDelegate
中设置 UITabBarController
委托(delegate)。然而,对于 Storyboards
,AppDelegate
不知道 tabBarController
在启动时的位置。您可以通过子类化
tabBarController
并添加委托(delegate)方法来思考:
(void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController {
}
... 就足够了。但是,令人恼火的是,事实并非如此。
我需要知道用户何时按下了标签按钮。我需要更多地了解这一点,我需要知道 viewController 的“- (void)viewWillDisappear:(BOOL)animated {}
"方法已经运行。
我决定让我的 UITabBarController
成为它自己的委托(delegate)。这对我来说似乎很愚蠢,但我做了以下......
#import <UIKit/UIKit.h>
@interface PlumbsTabBarController : UITabBarController <UITabBarControllerDelegate>
@end
然后,在我的 viewDidLoad
方法中,写下以下内容:
[self setDelegate:self];
这使我的选项卡栏委托(delegate)方法能够运行。
疯了还是什么?
好的 - 我现在正在编辑这个答案,因为即使以上都是正确的,在使用 navigationController
的地方,通过触摸每个 tabBarButton
选择, didSelectViewController
委托(delegate)方法将在您尝试 NSLog(@"%@", viewController);
时仅显示您已选择 UINavigationController
类(class)?
因此,为了增加更多的复杂性,总的解决方案是为每个要监视的 viewController
子类化 UINavigationController
,(做某事)当 tabBarbutton
已被触摸。
无论如何它对我有用。而且,如果有人可以通过上面的运球挑剔,他们可能会发现一个有用的方面 - 这对我来说就足够了 - 看到我发现这个网站也非常有用。
关于ios - 未调用 didSelectViewController 方法(使用 Storyboard ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065605/