我想触摸右上角的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'' 我该如何解决? 谢谢!
答案 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];