Search code examples
iosobjective-cuinavigationcontrollerstoryboard

Unable to navigate to UIViewController from AppDelegate


I am trying to navigate from AppDelegate didViewLoadFinished function but I'm unable to succeed as I'm completely new to ios. I'm using a storyboard for ViewControllers.

I also checked the other solutions like this one : Swift - pushViewController from appDelegate, rootViewController.navigationController is nil but it didn't solve my problem.

Here is my ViewController on story Board: StoryBoard screenshot

Here is how I'm trying to load new ViewController "MultipleMarkersViewController":

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main.storyboard" bundle:nil];
            MultipleMarkersViewController *wc=[storyboard instantiateViewControllerWithIdentifier:@"MultipleMarkersView"];
            [self.window.rootViewController.navigationController pushViewController:wc animated:YES];

The expected result is to load the new ViewController. But is the actual result is "The App crashes" with following log output:

0   CoreFoundation                      0x000000010894f6fb __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x0000000107ef3ac5 objc_exception_throw + 48
    2   UIKitCore                           0x00000001119f9624 +[UIStoryboard storyboardWithName:bundle:] + 675
    3   Runner                              0x00000001060b09bd __57-[AppDelegate application:didFinishLaunchingWithOptions:]_block_invoke + 909
    4   Flutter                             0x000000010614c9a2 __45-[FlutterMethodChannel setMethodCallHandler:]_block_invoke + 115
    5   Flutter                             0x0000000106169616 _ZNK5shell21PlatformMessageRou<…>

Solution

  • Here is how I resolved the issue after 24hrs of struggle. The only thing I tried was "Used Try Catch Block" and the error was that I was not using the correct identifier name which was causing null pointer exception. Here is the code:

    @try {
                    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    MultipleMarkersVC *vc = [sb instantiateViewControllerWithIdentifier:@"MultipleMarkersVC"];
                    UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:vc];
                    [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
                }
                @catch (NSException * e) {
                    NSLog(@"Exception: %@", e);
                }