Ios 在self.navigationController.viewControllers中重新排列viewControllers不起作用

Ios 在self.navigationController.viewControllers中重新排列viewControllers不起作用,ios,objective-c,iphone,uiviewcontroller,uinavigationcontroller,Ios,Objective C,Iphone,Uiviewcontroller,Uinavigationcontroller,比如说,在我的self.navigationController.viewControllers中,我有n个viewControllers。在它们中,我只希望在self.navigationController.viewControllers中保留2个viewControllers。一个位于0索引,另一个位于1索引。这是我的密码 NSMutableArray *VCs = [NSMutableArray arrayWithArray: self.navigationControl

比如说,在我的self.navigationController.viewControllers中,我有n个viewControllers。在它们中,我只希望在self.navigationController.viewControllers中保留2个viewControllers。一个位于0索引,另一个位于1索引。这是我的密码

        NSMutableArray *VCs = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
        NSMutableArray *savedVCsArray = [[NSMutableArray alloc] init];

        for (int i = 0; i < [VCs count]; i++) {
            UIViewController *vc = VCs[i];
            if ([vc isKindOfClass:[LanguageSettingsViewController class]]) {
                [savedVCsArray insertObject:vc atIndex:0];
            } else if ([vc isKindOfClass:[SignInViewController class]]) {
                [savedVCsArray insertObject:vc atIndex:1];
            }
        }

        [self.navigationController setViewControllers:savedVCsArray];
        NSLog(@"counts %i", [self.navigationController.viewControllers count]);
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
但是,我总是在这里NSLog@counts%i,[self.navigationController.ViewController计数];0是不是我遗漏了什么?提前谢谢。很高兴你能回复

xcode版本8.3.2 8E2002
iOS 10.3

确保您确实拥有所需的VCs:

UIViewController *langVC = nil;
UIViewController *signVC = nil;

for (UIViewController *vc in self.navigationController.viewControllers) {
    if ([vc isKindOfClass:[LanguageSettingsViewController class]]) {
        // [savedVCsArray insertObject:vc atIndex:0];
        langVC = vc;
    } else if ([vc isKindOfClass:[SignInViewController class]]) {
        // [savedVCsArray insertObject:vc atIndex:1];
        signVC = vc;
    }
}

if (langVC == nil || signVC == nil) {

    NSLog(@"Problem exists because langVC is %@ / signVC is %@", langVC, signVC);

} else {

    [self.navigationController setViewControllers:@[langVC, signVC] animated:YES];

}

self.navigationController的价值是什么?i、 它不是nil吗?它是指针类型的值0x00000000,不是,它不是nill。因此,换句话说,代码中的self对象似乎没有navigationController。您是否检查了VCs阵列中的内容?0x00000000非常类似于nil:哦,好的。是,在insertValue之前,它具有所有ViewController列表。像这样:谢谢你的回答。是的,我也尝试过使用您的代码,但它总是为您打印0NSLog@counts%i,[self.navigationController.ViewController计数];插入状态后。但是在我的SignenViewController中,当我打印[self.navigationController.ViewController计数]时,它给了我所需的计数,即2。因此,我认为代码中没有任何问题,相反,它可能有一些其他问题。啊-好吧,当您调用.setViewController时,转换是异步的。因此,当您立即检查计数时,似乎导航控制器已在内部清除堆栈,但尚未添加新的VCs阵列。如果您在转换完成后检查计数,您应该会看到正确的值。哦!因此,所有这些都是异步转换:非常感谢你告诉我细节。我对此有点困惑。祝你有个好的日伴