没有调用didSelectViewController方法(带故事板) [英] didSelectViewController method not being called (with storyboard)

查看:167
本文介绍了没有调用didSelectViewController方法(带故事板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个版本的选项卡式ios5应用程序,一个使用故事板创建,另一个使用xib文件。故事板版本不会调用 UITabBarControllerDelegate 方法 didSelectViewController (xib版本)。从故事板中遗漏了一些东西(我认为),但我不知道是什么。构建问题的另一种方法可能是 - 如何引用故事板实例化的 UITabBarController 对象?

I have 2 versions of a tabbed ios5 application, one created using a storyboard and one using xib files. The storyboard version does not call the UITabBarControllerDelegate method didSelectViewController (the xib version does). Something is (I think) missing from the storyboard, but I don't know what. Another way of framing the question might be — how can I refer to the UITabBarController object instantiated by the storyboard?

感谢您的帮助。

编辑:标签栏控制器委托已设置:

The tab bar controller delegate is set:

在AppDelegate.h中:

In AppDelegate.h:

@interface MyAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
@property (strong, nonatomic) UITabBarController *tabBarController;

在AppDelegate.m中:

In AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.tabBarController.delegate = self;
    return YES;
}

然后在AppDelegate.m中,委托方法为:

Then later in AppDelegate.m, the delegate method is:

- (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"Got Here");
}

NSLog输出永远不会出现。在我看来,问题是我没有正确引用已经由故事板实例化的标签栏控制器对象。
我该怎么做?

The NSLog output never appears. The problem seems to me to be that I am not correctly referencing the tab bar controller object which has been instantiated by the storyboard. How do I do that?

推荐答案

我有这个问题。

如果您没有使用故事板,请在 AppDelegate <中设置 UITabBarController 委托/ code>是要走的路。但是,使用 Storyboards AppDelegate 不知道 tabBarController 正在启动。您可以通过继承 tabBarController 并添加委托方法:

If you're not using storyboards, setting the UITabBarController delegate in the AppDelegate is the way to go. However, with Storyboards, the AppDelegate has no idea where the tabBarController is on startup. You'd think by subclassing the tabBarController and adding the delegate method:

(void)tabBarController:(UITabBarController *)tabBarController
   didSelectViewController:(UIViewController *)viewController {

}

......就足够了。但是,这令人恼火。

... would be enough. But, it's irritatingly not.

我需要知道用户何时按下了标签按钮。我需要知道更多,我需要知道viewController的 - (void)viewWillDisappear:(BOOL)动画{}
方法已经运行。

I needed to know when a user had pressed a tab button. I needed to know this more that I needed to know that the viewController's "- (void)viewWillDisappear:(BOOL)animated {} " method had been run.

我决定让我的 UITabBarController 成为自己的代表。这对我来说似乎很愚蠢,但我做了以下... ...

I decided to make my UITabBarController a delegate of itself. This seemed silly to me but I did the following...

#import <UIKit/UIKit.h>

@interface PlumbsTabBarController : UITabBarController <UITabBarControllerDelegate>

@end

然后,在我的中viewDidLoad 方法,写了以下内容:

And then, in my viewDidLoad method, wrote the following:

[self setDelegate:self];

这使我的标签栏委托方法能够运行。

Which enabled my tab bar delegate methods to run.

疯狂还是什么?

好的 - 我现在正在编辑这个答案,因为即使以上都是正确的,其中一个正在使用navigationController ,选中每个 tabBarButton 触摸, didSelectViewController 委托方法将,当你尝试 NSLog(@%@,viewController); 只显示你选择了 UINavigationController class?

Ok - I'm editing this answer now, as even though the above is all correct, where a navigationController is being used, selected with each tabBarButton touched, the didSelectViewController delegate method will, when you try to NSLog(@"%@", viewController); only show you that you have selected the UINavigationController class?

因此,为了增加更多复杂性,整个解决方案是为每个< UINavigationController 创建子类 viewController 当你触摸 tabBarbutton 时,你要监控,(做某事)。

So, the total solution, just to add more complexity, is to subclass the UINavigationController for each viewController that you want to monitor, (do something) when the tabBarbutton has been touched.

无论如何它对我有用。并且,如果任何人都可以通过上述运球挑选,他们可能会找到一个有用的方面 - 这对我来说足够了 - 因为我发现这个网站也非常有用。

It works for me anyhow. And, if anyone can nit-pick through the above dribble, they might find an aspect that's useful - and that's enough for me - seeing as I find this site utterly useful too.

这篇关于没有调用didSelectViewController方法(带故事板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆