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

我想触摸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]; } 

但是,当我运行’无法在捆绑中加载NIB时,XCode出错:’NSBundle …(已加载)’,名称为“chosenCountry”,如何解决? 谢谢!

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

由于您已在故事板中添加了vc,因此initWithNibName将不起作用。

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

使用此代码它适合您

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