【问题标题】:Could not load NIB in bundle: 'NSBundle ··· with name 'chosenCountry''无法在包中加载 NIB:'NSBundle ··· 名称为 'chosenCountry''
【发布时间】:2016-05-30 11:21:31
【问题描述】:

我想触摸右上角的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];
} 

但是当我运行'Could not load NIB in bundle:'NSBundle ...(loaded)' with name 'chosenCountry''时,XCode 出现错误 我该如何解决? 谢谢!

【问题讨论】:

  • 你有一个名为chosenCountry的xib文件吗?
  • 我认为您的 viewconrtoller 名称是 selectedViewController。试试这个 selectedViewController
  • @Larme 我不知道,我在 storyboard 中拖了一个新的视图控制器,并将它的类设置为 selectedViewController。我该怎么办?非常感谢
  • 那么,和那个有关:stackoverflow.com/questions/9896406/…?
  • @MAuLIK 如果将其更改为 selectedViewController,Xcode 甚至不让我运行

标签: ios objective-c


【解决方案1】:
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
 UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"chosenCountry"];
 [self.navigationController pushViewController:vc animated:YES];

【讨论】:

  • 欢迎@Shucheng :-)
【解决方案2】:

由于您已在情节提要中添加了 vc,因此 initWithNibName 将不起作用。

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

【讨论】:

    【解决方案3】:

    使用适合你的代码

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

    【讨论】:

    • 是的!它对我有用,但 Bhadresh Mulsaniyaa 先回答了,对不起:)
    猜你喜欢
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 2012-09-08
    • 2015-06-19
    相关资源
    最近更新 更多