What is the correct way to proceed with this bool?

2020-02-07 13:08发布

问题:

I have this bool in my AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

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

        UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];

        self.window.rootViewController = viewController;
        [self.window makeKeyAndVisible];

        return YES;
    }

I'm getting an error on the *storyboard but what I am mainly trying to do is initialize the application in a different view controller than the main one. I have all the view controllers with IDs (such as the one I want to launch is named "Home"). How would I correctly write this bool if say my ViewController ID was home?

My main.m:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

The nil I get is whenever I try to go back to the original Home page from my Contact list page so it crashes and highlights the nil line. Both this and the above delegate bool are giving me issues. Any clarification would be very much appreciated, thanks.

回答1:

Use this in your AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];



    HomeController *viewController = // initialise it 

    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

or in the storyBoard just put the pointing arror to the view controller which you want to make as a root view controller.



回答2:

The stack trace explains what's wrong:

  • Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'MainStoryboard' in bundle NSBundle (loaded)' *

Your storyboard isn't called "MainStoryboard".