2016-05-30 72 views
0

我想触摸右上角的UIBarButtonItem,然后推送新的viewController。所以,代码:无法在包中加载NIB:'NSBundle ... with name'chosenCountry''

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]; 
} 

但XCode中有一个错误,当我运行“捆绑无法加载NIB: '一个NSBundle ...(加载)'与名称'chosenCountry'' 我该如何解决它? 谢谢!

+0

你有一个名为'chosenCountry'一个XIB文件? – Larme

+0

我认为你的viewconrtoller名称是selectedViewController.try这个chosenViewController –

+0

@Larme我不知道,我拖动一个新的视图控制器在storyboard中,并将其设置在selectedViewController类中。我该怎么办?非常感谢你 – Shucheng

回答

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

它与我一起工作,非常感谢! – Shucheng

+0

欢迎@舒城:-) –

0

既然你添加了你的vc故事板,因此initWithNibName将无法​​正常工作。

要么使用segue或使用故事板的方法instantiateViewControllerWithIdentifier实例化视图控制器

+0

谢谢!我会试试! – Shucheng

0

使用此代码,它为你工作

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

是啊!它与我合作,但Bhadresh Mulsaniyaa首先回答,对不起:) – Shucheng