无法在捆绑中加载NIB:'NSBundle···名称'chosenCountry''

时间:2016-05-30 11:21:32

标签: ios objective-c

我想触摸右上角的UIBarButtonItem,然后推送一个新的viewController。所以,代码是:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
                              initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                              target:self
                              action:@selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;

和方法insertNewObject是:

-(void)insertNewObject{
    chosenViewController *chosenCountry = [[chosenViewController alloc]initWithNibName:@"chosenCountry" bundle:nil];
    //self.chosenViewController = chosenCountry;
    [self.navigationController pushViewController:chosenCountry animated:YES];
} 

但是当我运行'无法在bundle中加载NIB时'XCode出错:'NSBundle ...(已加载)',名称为'chosenCountry'' 我该如何解决? 谢谢!

3 个答案:

答案 0 :(得分:0)

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
 UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"chosenCountry"];
 [self.navigationController pushViewController:vc animated:YES];

答案 1 :(得分:0)

由于您已在故事板中添加了vc,因此initWithNibName将无效。

使用segue或使用storyboard的方法instantiateViewControllerWithIdentifier来实例化视图控制器

答案 2 :(得分:0)

使用适用于您的代码

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
yourViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"chosenCountry"];
[self.navigationController pushViewController:vc animated:YES];