使用popToRootViewController的UINavigationController问题

时间:2011-10-09 04:48:09

标签: iphone objective-c ipad uinavigationcontroller poptoviewcontroller

所以,让我说我在viewcontroller,C,我想转换到viewcontroller D(这里是voteView)。所以这就是我所做的:

VoteViewController * voteView = [[VoteViewController alloc] init];
        voteView.voteInfo = [array objectAtIndex:0];

    NSArray * viewControllers = [self.navigationController viewControllers];
    if ([[viewControllers objectAtIndex:0] isKindOfClass:[ListViewController class]]){
        NSLog(@"LIST VIEW");
    } else if ([[viewControllers objectAtIndex:0] isKindOfClass:[SpotListingViewController class]]){
        NSLog(@"SPOT LISTING");
    }

    [self.navigationController setViewControllers:[NSArray arrayWithObjects:[viewControllers objectAtIndex:0], [viewControllers objectAtIndex:1], voteView, nil]];
    [voteView release];

运行上面的代码,它打印LIST VIEW,这意味着RootViewController是一个ListViewController。现在我在VoteViewController上进行了如下操作:

 if ([[viewControllers objectAtIndex:0] isKindOfClass:[ListViewController class]]){
            NSLog(@"LIST VIEW");

它应该打印LIST VIEW,但它不会。为什么是这样?我还检查了视图控制器的计数,它只有2.为什么它不同?

1 个答案:

答案 0 :(得分:1)

如果您要求转换到View Controller D(或voteView),那么您应该这样做 -

[self.navigationController pushViewController:voteView animated:YES];

与您的问题相关,当您在索引1处调用setViewControllers:并访问对象时,您是否确保该数组有两个对象?您使用voteView的哪种方法NSLog