didSelectViewController在某些情况下不会被调用 [英] didSelectViewController does not get called on certain occasions

查看:111
本文介绍了didSelectViewController在某些情况下不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到许多已经报道过的问题, didSelectViewController 没有被调用,但在我的情况下,它有时会被调用。我有三个选项卡和三个视图控制器。每次用户按下第二个或第三个选项卡时,我都需要执行一些代码。在我的SecondViewController和ThirdViewController中,我有:

I have the problem that many already have reported, didSelectViewController doesn't get called, but in my case it sometimes gets called. I have three tabs and three view controllers. Every time user presses second or third tab I need to execute some code. In my SecondViewController and ThirdViewController I have:

UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
[tabBarController setDelegate:self];

现在一切正常,使用SecondViewController, didSelectViewController 。同样在ThirdViewController中每次按下第三个选项卡时都会调用didSelectViewController 但仅在没有按下第二个栏时调用。因此,当我在FirstViewController和ThirdViewController之间来回切换时,一切正常。但是当我进入第一个 - >第二个 - >第三个模式时,那么 didSelectViewController 不会在ThirdViewController中被调用。此外,当我第一次 - >第三 - >第二 - >第三 didSelectViewController 第一次在第三次调用时调用,但不是第二次。有什么想法?

Now everything works fine with the SecondViewController, the didSelectViewController gets called every time the second tab is pressed. Also in ThirdViewController didSelectViewControllergets called every time the third tab is pressed but only when second bar is meanwhile not pressed. So when I switch back and forth between FirstViewController and ThirdViewController everything is OK. But when I go in a pattern like first->second->third, then didSelectViewController doesn't get called in ThirdViewController. Also when I go like first->third->second->third didSelectViewController gets called in ThirdViewController the first time but not the second time. Any ideas?

推荐答案

很难理解你究竟在做什么,但据我所知,你正在回应制表符开关在 SecondViewController ThirdViewController UITabBarController 的委托c>。

It's hard to follow what exactly you are doing, but from what I understand you are responding to tab switches by changing the UITabBarController's delegate back and forth between SecondViewController and ThirdViewController.

如果确实如此,我建议不要这样做。相反,我建议你尝试以下方法:

If that is true, I would advise against doing this. Instead I would suggest you try the following:


  • 分配一个永不改变的代表。首先,您可以使用您的应用程序委托,但如果您有专门的小类可能会更好。我确信现在你有一个不变的委托,它将获得100%的所有调用 tabBarController:didSelectViewController:

  • 作为委托的对象必须引用 SecondViewController ThirdViewController 实例。如果您使用Interface Builder设计UI,可以通过向委托类添加两个 IBOutlet 并将相应的实例连接到出口来实现此目的。

  • 现在,当委托收到 tabBarController:didSelectViewController:时,它只需将通知转发到 SecondViewController ThirdViewController ,具体取决于选择的选项卡。

  • Assign a delegate that never changes. For a start you could use your app delegate, but it would probably be better if you had a dedicated small class for this. I am sure that now you have a non-changing delegate, it will get 100% of all the calls to tabBarController: didSelectViewController:.
  • The object that is the delegate must have a reference to both the SecondViewController and ThirdViewController instances. If you are designing your UI with Interface Builder, you might do this by adding two IBOutlets to the delegate class and connecting the appropriate instances to the outlets.
  • Now when the delegate receives tabBarController: didSelectViewController: it can simply forward the notification to either SecondViewController or ThirdViewController, depending on which of the tabs was selected.

基本代码示例:

// TabBarControllerDelegate.h file
@interface TabBarControllerDelegate : NSObject <UITabBarControllerDelegate>
{
}

@property(nonatomic, retain) IBOutlet SecondViewController* secondViewController;
@property(nonatomic, retain) IBOutlet ThirdViewController* thirdViewController;


// TabBarControllerDelegate.m file
- (void) tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController
{
    if (viewController == self.secondViewController)
      [self.secondViewController doSomething];
    else if (viewController == self.thirdViewController)
      [self.thirdViewController doSomethingElse];
}






编辑

有关如何将上面的示例代码集成到项目中的一些提示:

Some hints on how to integrate the example code from above into your project:


  • TabBarControllerDelegate 的实例添加到.xib文件中,该文件还包含 TabBarController

  • TabBarController '的委托出口连接到 TabBarControllerDelegate instance

  • 连接第二层视图 的插座> TabBarControllerDelegate SecondViewController 实例

  • 连接 thirdViewController 出口 TabBarControllerDelegate ThirdViewController 实例

  • 添加方法 - (void )doSomething SecondViewController

  • 添加方法 - (void)doSomethingElse ThirdViewController

  • 确保 SecondViewController 中没有任何代码, ThirdViewController 更改 TabBarController 委托!

  • Add an instance of TabBarControllerDelegate to the .xib file that also contains the TabBarController
  • Connect the delegate outlet of TabBarController' to the TabBarControllerDelegate instance
  • Connect the secondViewController outlet of TabBarControllerDelegate to the SecondViewController instance
  • Connect the thirdViewController outlet of TabBarControllerDelegate to the ThirdViewController instance
  • Add a method - (void) doSomething to SecondViewController
  • Add a method - (void) doSomethingElse to ThirdViewController
  • Make sure that you don't have any code left in SecondViewController and ThirdViewController changes the TabBarController delegate!

一旦你完成所有工作并且一切正常,你可能会想要清理一下:

Once you are all set and everything is working fine, you will probably want to cleanup a bit:


  • 更改通知方法的名称 doSomething doSomethingElse 更明智的事情

  • 如果您按照评论中的讨论进行,也许您还想摆脱 secondViewController thirdViewController outlets

  • Change the names of the notification methods doSomething and doSomethingElse to something more sensible
  • If you followed the discussion in the comments, maybe you also want to get rid of the secondViewController and thirdViewController outlets

这篇关于didSelectViewController在某些情况下不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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