0

I want touch the UIBarButtonItem in the top right, and push a new viewController. So, the code is:

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

and method insertNewObject is:

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

but XCode have a error when I run 'Could not load NIB in bundle: 'NSBundle ...(loaded)' with name 'chosenCountry'' How can I fix it? Thanks!

Larme
  • 24,190
  • 6
  • 51
  • 81
Koiost
  • 105
  • 1
  • 11
  • Do you have a xib file named `chosenCountry`? – Larme May 30 '16 at 11:24
  • i think your viewconrtoller name is chosenViewController.try this chosenViewController – Maulik shah May 30 '16 at 11:24
  • @Larme I don't know,I drag a new view controller in storyboard and set it's class at chosenViewController.What should I do?thank you very much – Koiost May 30 '16 at 11:28
  • Then, it's related to that: http://stackoverflow.com/questions/9896406/how-can-i-load-storyboard-programmatically-from-class ? – Larme May 30 '16 at 11:29
  • @MAuLIK if change it to chosenViewController,Xcode even don't let me run – Koiost May 30 '16 at 11:29

3 Answers3

0
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
 UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"chosenCountry"];
 [self.navigationController pushViewController:vc animated:YES];
Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25
0

Since you have added your vc in storyboard, hence initWithNibName will not work.

Either use segue or use storyboard's method instantiateViewControllerWithIdentifier to instantiate the view controller

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
0

Use this code it works for you

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