草庐IT

ios - didSelectViewController 在某些情况下不会被调用

coder 2024-01-14 原文

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

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

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

最佳答案

很难理解您到底在做什么,但据我了解,您是通过在 SecondViewController 之间来回更改 UITabBarController 的委托(delegate)来响应选项卡切换和 ThirdViewController

如果这是真的,我建议不要这样做。相反,我建议您尝试以下操作:

  • 分配一个永不改变的代表。一开始你可以使用你的应用委托(delegate),但如果你有一个专门的小类可能会更好。我确信现在您有一个不变的委托(delegate),它将获得对 tabBarController: didSelectViewController: 的所有调用的 100%。
  • 作为委托(delegate)的对象必须引用 SecondViewControllerThirdViewController 实例。如果您使用 Interface Builder 设计 UI,您可以通过向委托(delegate)类添加两个 IBOutlet 并将适当的实例连接到 socket 来实现。
  • 现在,当委托(delegate)收到 tabBarController: didSelectViewController: 时,它可以简单地将通知转发给 SecondViewControllerThirdViewController,具体取决于选项卡被选中。

基本代码示例:

// 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];
}

编辑

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

  • TabBarControllerDelegate 实例添加到还包含 TabBarController 的 .xib 文件中
  • TabBarControllerdelegate 导出连接到 TabBarControllerDelegate 实例
  • TabBarControllerDelegatesecondViewController 导出连接到 SecondViewController 实例
  • TabBarControllerDelegatethirdViewController 导出连接到 ThirdViewController 实例
  • 添加方法 - (void) doSomethingSecondViewController
  • 添加方法 - (void) doSomethingElseThirdViewController
  • 确保 SecondViewControllerThirdViewController 中没有任何代码会更改 TabBarController 委托(delegate)!

一旦一切就绪并且一切正常,您可能需要稍微清理一下:

  • 将通知方法 doSomethingdoSomethingElse 的名称更改为更合理的名称
  • 如果您关注了评论中的讨论,也许您还想摆脱 secondViewControllerthirdViewController socket

关于ios - didSelectViewController 在某些情况下不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17534330/

有关ios - didSelectViewController 在某些情况下不会被调用的更多相关文章

  1. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

  2. ruby - 默认情况下使选项为 false - 2

    这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb

  3. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  4. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  5. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  6. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  7. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  8. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  9. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  10. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

随机推荐