ios - 未调用 didSelectViewController 方法(使用 Storyboard )

标签 ios ios5 uitabbarcontroller storyboard

我有 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)。然而,对于 StoryboardsAppDelegate 不知道 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/

相关文章:

ios - 当从 TabbarController 插入 SplitviewController 时,UIView 自动向上移动

iphone - iPhone中textfieldshouldbeginediting和textfieldDidendediting的区别

c# - 如何在 MonoTouch 中为继承 View 创建 UIAppearance 代理?

ios - Swift – UITabBar.appearance().barTintColor 在模拟器中不起作用

ios - UITabBar tintColor 未更改 Storyboard

iphone - 如何在滚动 uiscrollview 时更改图像背景?

ios - UIButton底部出现白线

android - 如何在 React Native 中使用数据库?

ios - 适用于 iOS 6 的新 Facebook SDK 在 iOS 5 中存在崩溃问题

swift - UITabBarController: UITabBarItem 更新它或重新加载它