ios - 弹出以在导航堆栈中查看 Controller

标签 ios objective-c uistoryboardsegue

我正在尝试弹出导航堆栈中的 View Controller 。

这是我的 Storyboard的样子。

enter image description here

我的 TableView Controller 有 4 个单元格。当我单击第四个单元格时,我想弹出到最后一个 View Controller 。

#import "TableViewController.h"
#import "ViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = [NSString stringWithFormat:@"Cell %ld", (long)indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    switch (indexPath.row) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            NSLog(@"%@", [NSString stringWithFormat:@"Cell %li tapped", (long)indexPath.row]);

            NSLog(@"%lu", (unsigned long)self.navigationController.viewControllers.count);

            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            ViewController *VC4 = [storyboard instantiateViewControllerWithIdentifier:@"VCFour"];
            [self.navigationController popToViewController:VC4 animated:NO];

            break;
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

错误

2014-11-13 13:40:58.942 Test[18601:1828547] Cell 0 tapped
2014-11-13 13:40:58.942 Test[18601:1828547] 1
2014-11-13 13:40:58.943 Test[18601:1828547] *** Assertion failure in -[UINavigationController popToViewController:transition:], /SourceCache/UIKit_Sim/UIKit-3318/UINavigationController.m:5568
2014-11-13 13:40:58.945 Test[18601:1828547] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to get popped view controller.'

如您所见,self.navigationController.viewControllers.count 将 1 打印到控制台,也许这就是问题所在。这不应该是 5,因为 Storyboard上总共有 5 个 View Controller 吗?

有人能帮忙吗?

最佳答案

您的导航堆栈只包含已经推送到它上面的 Controller (这只会在实际运行 segue 时发生)。您是否已将它们设置在 Storyboard中并不重要。如果您在 Controller 1 上并且想要访问 Controller 4,您可以通过 self.navigationCotnroller pushViewController:(或通过运行 segue)推送 Controller 4,或者您可以在 Controller 上设置一个标志然后推送 Controller 2,它将检查标志并推送 Controller 3,它将检查标志并推送 Controller 4。这种方法更好,因为如果需要,您可以弹回 Controller 2/3,并且您的堆栈与您设计的一样.

关于ios - 弹出以在导航堆栈中查看 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26910400/

相关文章:

iOS - 如何计算特定 View 内的点击次数?

ios - 在 View Controller 之间传递数据

ios - 在多个子类之间共享 Storyboard View Controller

ios - 如何将选定的图像从 UIImagePickerController 传递到第二个 View Controller ?

ios - 在 xamarin for ios 上调试时,找不到应用程序的有效 'aps-environment' 权利字符串

iphone - uitableview 上下滚动时复选框状态改变

ios - 应用程序传输安全 Xcode 7 beta 6

ios - NSXMLParser在<?xml version =“1.0”?>上引发NSXMLParserErrorDomain#64

iphone - 何时删除 iOS 钥匙串(keychain)项目?

ios - 如何关闭我用 "replace"segue 打开的 View Controller