无法在名为“SecondViewController”的包中加载NIB

时间:2012-09-16 00:02:10

标签: objective-c xcode pushviewcontroller

我正在尝试将Navigator Controller推送到另一个视图控制器,但我不断收到错误,说明无法加载nib包名称。这是我正在使用的代码:

SecondViewController *vc1 = [[SecondViewController alloc]
      initWithNibName:NSStringFromClass([SecondViewController class]) bundle:Nil];
[self.navigationController pushViewController:vc1 animated:YES];

1 个答案:

答案 0 :(得分:1)

您必须传递nib文件的名称,而不是类名:

// Assuming there is a properly set up SecondViewController.xib in your project.
SecondViewController *vc1 = [[SecondViewController alloc] 
                            initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:vc1 animated:YES];

修改

如果您正在使用Storyboard,则必须以不同方式加载GUI:

UIStoryboard *sboard = [UIStoryboard storyboardWithName:@"StoryboardFileName" 
                                                 bundle:nil];
SecondViewController *vc1 = [sboard instantiateInitialViewController];