iOS ߝ显示导航堆栈中已有的视图控制器

发布于 2025-01-04 12:51:26 字数 771 浏览 2 评论 0原文

这可能很容易解决,但我无法弄清楚。我有一个带有两个选项卡的选项卡栏应用程序,每个选项卡都有一个 UINavigationController 。

假设我在选项卡 1 中推送一个名为 ItemViewController 的视图控制器,然后转到选项卡 2。我想从选项卡 2 以编程方式显示 ItemViewController。因此,它应该首先将我带到选项卡 1,然后显示 ItemViewController

只需点击选项卡 1 选项卡项即可轻松实现此目的,但出于其他原因我想以编程方式执行此操作。

我现在正在做什么来实现这一目标:

[tab1NavController popToRootViewControllerAnimated:NO];
[tabBarController setSelectedIndex:0];
[tab1NavController pushViewController:itemViewController animated:NO];

我希望能够用伪代码做这样的事情:

if(viewControllerIWantToDisplayIsOnNavStack)
[tab1NavController presentViewController:viewControllerIWantToDisplay];
else
//instantiate and pushviewcontroller onto stack

我怎样才能实现这一目标?

This is probably easily sorted but I can't figure it out. I have a tab bar application with two tabs, each tab has a UINavigationController.

Let's say that I in tab 1 push a viewcontroller called ItemViewController, then I go to tab 2. From tab 2 I want to programatically display the ItemViewController. So it should first take me to tab 1 and then display the ItemViewController.

This is easily achievable by just tapping on the tab 1 tab item but I want to do this programtically for other reasons.

What I'm doing right now to achieve this:

[tab1NavController popToRootViewControllerAnimated:NO];
[tabBarController setSelectedIndex:0];
[tab1NavController pushViewController:itemViewController animated:NO];

I would like to be able to do something like this in pseudo-code:

if(viewControllerIWantToDisplayIsOnNavStack)
[tab1NavController presentViewController:viewControllerIWantToDisplay];
else
//instantiate and pushviewcontroller onto stack

How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

递刀给你 2025-01-11 12:51:26
// check if the desired controller is on the stack in tab 1
if([[tab1NavController viewControllers] containsObject:viewControllerIWantToDisplay]) {
    // desired controller is on the stack, let's see if it's on top
    if(tab1NavController.topViewController == viewControllerIWantToDisplay) {
        // no need to do anything
        }
    else {
        // we need to pop to the desired view controller
        [tab1NavController popToViewController:viewControllerIWantToDisplay animated:NO];
    }
} else {
    // desired controller not on the stack
    [tab1NavController pushViewController:viewControllerIWantToDisplay animated:NO];
}

因此,您不必再弹出到选项卡 1 中的根视图控制器。

// check if the desired controller is on the stack in tab 1
if([[tab1NavController viewControllers] containsObject:viewControllerIWantToDisplay]) {
    // desired controller is on the stack, let's see if it's on top
    if(tab1NavController.topViewController == viewControllerIWantToDisplay) {
        // no need to do anything
        }
    else {
        // we need to pop to the desired view controller
        [tab1NavController popToViewController:viewControllerIWantToDisplay animated:NO];
    }
} else {
    // desired controller not on the stack
    [tab1NavController pushViewController:viewControllerIWantToDisplay animated:NO];
}

So you don't have to pop to root view controller in the tab 1 any more.

彡翼 2025-01-11 12:51:26

对我来说,以下方法有效,我使用循环来检查类是否在堆栈数组中

 NSInteger viewControllersCount = 0;
    UIViewController *rootViewController = nil;
    Yourcontroller *viewController = nil;

    NSArray *viewControllers = self.navigationController.viewControllers;

    viewControllersCount = viewControllers.count - 1;

    for (int i = viewControllersCount ; i > 0 ; i--) {
        rootViewController = [viewControllers objectAtIndex:i];
        if([rootViewController isKindOfClass:[Yourcontroller class]])
        {
            viewController = (NWAAccountViewC *)rootViewController;
            [self.navigationController popToViewController:rootViewController animated:YES];
            break;
        }

    }

for me following way worked, i used loop to check if class is in stack array

 NSInteger viewControllersCount = 0;
    UIViewController *rootViewController = nil;
    Yourcontroller *viewController = nil;

    NSArray *viewControllers = self.navigationController.viewControllers;

    viewControllersCount = viewControllers.count - 1;

    for (int i = viewControllersCount ; i > 0 ; i--) {
        rootViewController = [viewControllers objectAtIndex:i];
        if([rootViewController isKindOfClass:[Yourcontroller class]])
        {
            viewController = (NWAAccountViewC *)rootViewController;
            [self.navigationController popToViewController:rootViewController animated:YES];
            break;
        }

    }
傲鸠 2025-01-11 12:51:26

以编程方式选择另一个选项卡非常容易,只需执行以下操作:

tabbarcontroller.selectedIndex = tabNr;

但我看到您已经知道这一点。您可以通过如下检查来检查堆栈上的顶部视图控制器是否是您需要的视图控制器类型:

if([navigationcontroller.topviewcontroller isKindOfClass:[Yourcontroller class]])
{
    //change tabbar or something else
}

it's very easy to programmatically select another tab, just do this:

tabbarcontroller.selectedIndex = tabNr;

But I see you already know this. You can check whether the top view controller on the stack is the type of viewcontroller you need by checking like this:

if([navigationcontroller.topviewcontroller isKindOfClass:[Yourcontroller class]])
{
    //change tabbar or something else
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文