-1

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<…>
Haroon khan
  • 1,018
  • 2
  • 15
  • 27
  • 3
    You should use only "Main" for storyboard name. Replace "Main.storyboard" to "Main". – Muhammad Zohaib Ehsan May 03 '19 at 16:18
  • Now the crash is resolved but the app does not navigate to the next ViewController. What can be the possible reason. – Haroon khan May 03 '19 at 16:32
  • Actually I'm complete new to IOS and I have to implement it in Objective-C. – Haroon khan May 04 '19 at 05:23
  • Why did you need to navigate from appdelegate like this. If you need to show MultipleMarkersViewController as first view controller after launching app. Then you can easily do it by set to as initial view controller that's it. Or if you want to navigate from a rootviewcotnroller , Then first check that whether the root view controller has UInavigationcontroller or not. – Masum Biswas May 04 '19 at 13:31
  • Thank you so much! The Issue has been resolved. – Haroon khan May 04 '19 at 16:28

2 Answers2

0

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);
            }
Haroon khan
  • 1,018
  • 2
  • 15
  • 27
0

When you are loading a storyboard, there is no need to add the extension ".storyboard" to the name of that. So, please use the following:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

And about your last line, I would prefer to use the following line, if you are loading your view from [AppDelegate didFinishLaunchingWithOptions]:

[self.window setRootViewController: wc]
ashkan84f
  • 11
  • 1