Objective-C instantiateViewControllerWithIdentifier返回nil
我在一周后打开了我的项目,看起来,对于我在StoryBoard
创建的所有新的UIViewController
, instantiateViewControllerWithIdentifier
返回的是nil。 已经在项目中的所有ViewControllers
都正常工作。
GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"]; navigationController.viewControllers = @[gchCVC];
第一行返回nil,我最初的想法是self.storyboard
返回nil,所以我尝试了这个并放了断点。
if (self.storyboard != nil) { GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"]; navigationController.viewControllers = @[gchCVC]; }
它进入if并在第二行崩溃..
我还正确添加了类和storyboard
ID。
如果我尝试加载已经存在的另一个ViewController
,它可以工作,但不是新的。
似乎无法弄清楚是什么问题。 任何帮助表示赞赏。
编辑:
完全用法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; // Scroll to the top when tapped NSIndexPath *ind = [NSIndexPath indexPathForRow:0 inSection:0]; [tableView scrollToRowAtIndexPath:ind atScrollPosition:UITableViewScrollPositionBottom animated:NO]; NavigationViewController *navigationController = (NavigationViewController *)self.mm_drawerController.centerViewController; if (indexPath.section == 0) { if (indexPath.row == 0) { DailyCheckViewController *dailyCheckViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"dailyCheckViewController"]; navigationController.viewControllers = @[dailyCheckViewController]; }else if (indexPath.row == 1) { ECGViewController *ecgViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ecgViewController"]; navigationController.viewControllers = @[ecgViewController]; }else if (indexPath.row == 2) { SPO2ViewController *spo2ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"spo2ViewController"]; navigationController.viewControllers = @[spo2ViewController]; }else if (indexPath.row ==3) { TempViewController *tempViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"tempViewController"]; navigationController.viewControllers = @[tempViewController]; }else if (indexPath.row == 4) { SLMViewController *slmViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"slmViewController"]; navigationController.viewControllers = @[slmViewController]; }else if (![UserList instance].isSpotCheck && indexPath.row == 5) { PedViewController *pedViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"pedViewController"]; navigationController.viewControllers = @[pedViewController]; } }else if(indexPath.section == 1){ if (indexPath.row == 1) { AboutCheckmeViewController* aboutCheckmeViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"aboutCheckmeViewController"]; navigationController.viewControllers = @[aboutCheckmeViewController]; }else if (indexPath.row == 3) { AboutViewController* aboutViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"aboutViewController"]; navigationController.viewControllers = @[aboutViewController]; }else if (indexPath.row == 2) { SettingsViewController* settingsViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"settingsViewController"]; navigationController.viewControllers = @[settingsViewController]; }else if (indexPath.row == 0) { GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"]; navigationController.viewControllers = @[gchCVC]; } } [self.mm_drawerController setCenterViewController:navigationController withCloseAnimation:YES completion:nil]; }
我有一个导航抽屉。 切换到其他ViewController,但具有标识符’GchConnect’的那个(即indexPath.section == 1和indexpath.row == 0)工作。 它们都在同一个故事板中
如果您有多个故事板, self.storyboard
将返回当前UIViewController
所在的故事板。您需要检查“GchConnect”的storybaord名称。 并使用storyboardWithName:bundle实例化该故事板:
UIStoryboard storyboard = [UIStoryboard storyboardWithName:bundle:nil]; GCHConnectViewController* gchCVC = [storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];
使用此代码
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; HomepageViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"GchConnect"]; [self.navigationController pushViewController:vc animated:YES];
代替
if (self.storyboard != nil) { GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"]; navigationController.viewControllers = @[gchCVC]; }
尝试使用:
if (self.storyboard != nil) { GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"]; navigationController.pushViewController(gchCVC, animated: true) }
你应该做的是推或现视图控制器。 目前您正在尝试更换导航控制器中的所有视图控制器,
试试这个代码,
如果您使用下面的代码提供viewcontroller
,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @"YourStoryBoardName" bundle:[NSBundle mainBundle]]; GCHConnectViewController* gchCVC = [storyboard instantiateViewControllerWithIdentifier:@"GchConnect"]; [self presentViewController:gchCVC animated:YES completion:nil];
或者如果你推动viewcontroller
使用下面的代码,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @"YourStoryBoardName" bundle:[NSBundle mainBundle]]; GCHConnectViewController* gchCVC = [storyboard instantiateViewControllerWithIdentifier:@"GchConnect"]; [self.navigationController pushViewController:gchCVC animated:YES];
希望它有所帮助
我的问题是我添加了本地化,我添加到故事板的更改正在更新到模拟器/设备。
它通过以下方式修复:
然后选择StoryBoard
编辑器>本地化锁定>重置锁定控件