Root viewcontroller - 重定向到特定视图会引发异常

时间:2012-11-23 15:37:30

标签: iphone objective-c xcode cocoa-touch

我的应用程序的更新需要一个设置,用户可以选择要加载的视图。 所以,由于我正在使用故事板,我设法创建了一个视图控制器,它通过故事板连接到另一个视图,充当“根视图控制器”

然后我将以下代码添加到该视图中,我在这里使用示例字符串来测试代码是否有效。

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *test = @"one";

    id rootController;
    if (test == @"one") {
        rootController = [self.storyboard instantiateViewControllerWithIdentifier:@"one"];


    } else if (test == @"two") {
        rootController = [self.storyboard instantiateViewControllerWithIdentifier:@"two"];

    }
    else if (test == @"three") {
        rootController = [self.storyboard instantiateViewControllerWithIdentifier:@"three"];

    }

    else {
        rootController = [self.storyboard instantiateViewControllerWithIdentifier:@"none"];

    }
    self.view = [NSArray arrayWithObjects:rootController, nil];
}

但只要应用程序启动,就会抛出以下异常:

2012-11-23 16:30:40.482 MyApp[1587:c07] -[__NSArrayI _setViewDelegate:]: unrecognized selector sent to instance 0x716cf30
2012-11-23 16:30:40.501 MyApp[1587:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI _setViewDelegate:]: unrecognized selector sent to instance 0x716cf30'
*** First throw call stack:
(0x1c8e012 0x10cbe7e 0x1d194bd 0x1c7dbbc 0x1c7d94e 0xf85ac 0xf4a90 0x2dee 0xf4817 0xf4882 0xf4b2a 0x10bef5 0x10bfdb 0x10c286 0x10c381 0x10ceab 0x10cfc9 0x10d055 0x2123ab 0x6392d 0x10df6b0 0x228afc0 0x227f33c 0x228aeaf 0x1028cd 0x4b1a6 0x49cbf 0x49bd9 0x48e34 0x48c6e 0x49a29 0x4c922 0xf6fec 0x43bc4 0x43dbf 0x43f55 0x4cf67 0x10fcc 0x11fab 0x23315 0x2424b 0x15cf8 0x1be9df9 0x1be9ad0 0x1c03bf5 0x1c03962 0x1c34bb6 0x1c33f44 0x1c33e1b 0x117da 0x1365c 0x21dd 0x2105)
libc++abi.dylib: terminate called throwing an exception

为什么代码不起作用?

1 个答案:

答案 0 :(得分:1)

如果您正在使用Storyboard,您还应该使用Segues,它与Story5 for iOS5同时推出。

在您的MainStoryboard.storyboard中,您将拥有您的根ViewController和另外三个ViewControllers。选择你的根ViewController,ctrl +左键单击并拖动到另一个ViewControllers并释放。你会看到一个黑色的小对话框,上面写着Manual Segue(发音为seg-way),选择Modal。

您现在会看到连接这两个ViewControllers的线路。单击该行并在右侧工具栏(检查器)中,选择“属性检查器”选项卡按钮。给segue提供标识符" One"。

现在在Root View控制器中,在ViewDidLoad方法中添加以下方法:

  if ([test isEqualToString:@"one"]) {
        [self performSegueWithIdentifier:@"one" sender:nil];
    }