UINavigationController 可见视图控制器

发布于 2024-12-08 03:42:36 字数 590 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏蝶舞 2024-12-15 03:42:36

尝试使用 topViewController 而不是 visibleViewController

FirstViewController *fVC = [navController topViewController];

Try topViewController instead of visibleViewController.

FirstViewController *fVC = [navController topViewController];
谁的新欢旧爱 2024-12-15 03:42:36

根据您在问题和评论中的解释,我了解到您的代码尝试访问 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 your UINavigationController, 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 mentioned viewDidLoad 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 from visibleViewController and if it is not what expected, then instantiate the view properly.

I hope this helps.

泛泛之交 2024-12-15 03:42:36

我知道这个问题已经得到解答,但我找到了另一个可能有用的解决方案。就我而言,我对 NavigationController 中的某些 viewController 的旋转处理方式不同,我执行了以下操作:

子类 UINavigationController,然后在新子类中需要时,您可以访问当前的visibleViewController 的标题,如下所示:

- (BOOL)shouldAutorotate
{
    if ([[self visibleViewController].title isEqualToString:@"Special Case"]) {
        return NO;
    }
    return YES;
}

这不是特定于旋转的,这只是我用它做什么。您唯一要做的就是为您在 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:

- (BOOL)shouldAutorotate
{
    if ([[self visibleViewController].title isEqualToString:@"Special Case"]) {
        return NO;
    }
    return YES;
}

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 their viewDidLoad, if they are set in IB or are not set they will be nil.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文