UINavigationController 可见视图控制器
我有一个 UITabBarController,其中一个选项卡是 UINavigationController。我有一个搜索栏,可以转到 UINavigationController 中的某个视图。问题是,如果第一个视图不是由 UINavigationController 推送的,那么它会崩溃,因为我的搜索无法识别此调用中的visibleViewController:
UINavigationController *navController = [self.MainTab.viewControllers objectAtIndex:1];
FirstViewController *fVC = [navController visibleViewController];
我不明白的是,在这段代码之前,我这样做:
self.MainTab.selectedIndex = 1;
这段代码它自己选择该选项卡中的 viewController,然后视图将加载到我的知识中。那么这对于 [navControllervisibleViewController] 获取当前 viewController 来说还不够吗?谢谢。
I have a UITabBarController, and one tab is a UINavigationController. I have a search bar that goes to a certain view within the UINavigationController. The problem is that if the first view is not pushed by the UINavigationController, than it crashes because my search doesn't recognize the visibleViewController from this call:
UINavigationController *navController = [self.MainTab.viewControllers objectAtIndex:1];
FirstViewController *fVC = [navController visibleViewController];
What I don't understand is, before this code, I do this:
self.MainTab.selectedIndex = 1;
This code on its own selects the viewController in that tab, where then the view gets loaded to my knowledge. So shouldn't this be enough for the [navController visibleViewController] to get the current viewController? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 topViewController 而不是 visibleViewController。
Try topViewController instead of visibleViewController.
根据您在问题和评论中的解释,我了解到您的代码尝试访问
FirstViewController
类型的对象,据说是要推送到UINavigationController
上的第一个视图,当它还没有被创建时。另一方面,如果您首先以编程方式选择该选项卡,则会创建视图并且一切正常。事实上,该视图是在选择选项卡时运行的 viewDidLoad 方法中创建的。
我建议的解决方案是避免直接从搜索选项卡访问
UINavigationController
visibleViewController
;相反,让您的搜索代码访问您的应用程序的模型(如模型-视图-控制器中)并将结果存储在那里;然后,从前面提到的 viewDidLoad 方法再次访问模型以读取搜索结果并更新/显示 UI。在我看来,这是干净的解决方案。如果您想要对当前设计采取某种解决方法,请检查从
visibleViewController
返回的fVC
值,如果它不是预期的值,则正确实例化视图。我希望这有帮助。
From what you explain in your question and comments, I understand that your code tries to access an object of type
FirstViewController
, supposedly the first view to be pushed on to yourUINavigationController
, when it has not yet been created.On the other hand, if you first programmatically select the tab, the view is created and everything works fine. Indeed, that view is created in a
viewDidLoad
method that is run when the tab is selected.The solution I would suggest is avoiding accessing the
UINavigationController
visibleViewController
directly from your search tab; instead, let your search code access the model (as in Model-View-Controller) for your app and store there the result; then, from the mentionedviewDidLoad
method again access the model to read the search result and update/show the UI.This is the clean solution, IMO. If you want a sort of workaround to your current design, then check the
fVC
value you get back fromvisibleViewController
and if it is not what expected, then instantiate the view properly.I hope this helps.
我知道这个问题已经得到解答,但我找到了另一个可能有用的解决方案。就我而言,我对 NavigationController 中的某些 viewController 的旋转处理方式不同,我执行了以下操作:
子类 UINavigationController,然后在新子类中需要时,您可以访问当前的visibleViewController 的标题,如下所示:
这不是特定于旋转的,这只是我用它做什么。您唯一要做的就是为您在
viewDidLoad
中检查的每个 viewController 设置您的self.title
(如果它们在 IB 中设置或未设置)它们将为nil
。I know this has been answered, but I found another solution that might be helpful. In my case I was handling rotation differently for some viewControllers within my NavigationController, I did the following:
Subclass UINavigationController, then where needed in your new subclass you can access the current visibleViewController's title like so:
This is not specific to rotation, this is just what I used it for. The only thing you have to do is set your
self.title
for each of the viewControllers you are checking against in theirviewDidLoad
, if they are set in IB or are not set they will benil
.