Objective-C instantiateViewControllerWithIdentifie

2019-07-16 10:07发布

I opened my project after a week and it seems, for all the new UIViewController I create in the StoryBoard, instantiateViewControllerWithIdentifier is returning is nil. All the ViewControllers which are already in the project are working fine.

GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];
navigationController.viewControllers = @[gchCVC];

First line is returning nil, my initial thought was that self.storyboard is returning nil, so I tried this and put breakpoint.

if (self.storyboard != nil) {
    GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];
    navigationController.viewControllers = @[gchCVC];
}

and its going to inside the if and crash at the second line..

I've also added the class and storyboard id correctly.

enter image description here

If I try to load another ViewController which was already there, it works, but not new ones.

Can't seem to figure out what is the problem. Any help is appreciated.

EDIT:

full usage:

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

I've a navigation drawer. switching to other ViewController except with the one with identifier 'GchConnect' (ie. indexPath.section == 1 and indexpath.row == 0 ) works. They are all in the same storyboard

5条回答
等我变得足够好
2楼-- · 2019-07-16 10:46

instead of

if (self.storyboard != nil) {
    GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];
    navigationController.viewControllers = @[gchCVC];
}

try to use:

if (self.storyboard != nil) {
    GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];
    navigationController.pushViewController(gchCVC, animated: true)
}

What you should do is either push or present view controller. At the moment you're trying to replace all viewcontrollers that are in your navigationcontroller,

查看更多
贼婆χ
3楼-- · 2019-07-16 10:50

My problem was that I add localization and changes I add to the storyboard were getting updated to the simulator/device.

It fixed it by:

selecting the StoryBoard, then

Editor > Localization Locking > Reset Locking Controls

查看更多
SAY GOODBYE
4楼-- · 2019-07-16 11:03

If you have multiple storyboards, self.storyboard returns the one that the current UIViewController is in. You need you check the name of storybaord for "GchConnect". And instantiate that storyboard with storyboardWithName:bundle:

UIStoryboard storyboard = [UIStoryboard storyboardWithName:bundle:nil];
GCHConnectViewController* gchCVC = [storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];
查看更多
欢心
5楼-- · 2019-07-16 11:03

use this code

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
HomepageViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"GchConnect"];
[self.navigationController pushViewController:vc animated:YES];
查看更多
贼婆χ
6楼-- · 2019-07-16 11:13

Try this code,

if you present the viewcontroller use below code,

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                                @"YourStoryBoardName" bundle:[NSBundle mainBundle]];

GCHConnectViewController* gchCVC = [storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];

[self presentViewController:gchCVC animated:YES completion:nil];

or if you Push the viewcontroller use below code,

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                                    @"YourStoryBoardName" bundle:[NSBundle mainBundle]];

GCHConnectViewController* gchCVC = [storyboard instantiateViewControllerWithIdentifier:@"GchConnect"];

[self.navigationController pushViewController:gchCVC animated:YES];

hope its helpful

查看更多
登录 后发表回答