iOS - 在导航堆栈中显示视图控制器

时间:2012-02-10 12:51:51

标签: ios uinavigationcontroller uitabbarcontroller

这可能很容易分类,但我无法弄清楚。我有一个带有两个标签的标签栏应用程序,每个标签都有一个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

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:8)

// 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中弹出根视图控制器。

答案 1 :(得分:1)

对于我的方式工作,我使用循环来检查类是否在堆栈数组

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

    }

答案 2 :(得分:0)

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

tabbarcontroller.selectedIndex = tabNr;

但我知道你已经知道了。你可以检查堆栈上的顶视图控制器是否是你需要的viewcontroller类型,如下所示:

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