简体   繁体   中英

Is there a way to have the App Delegate run the conditional and then chose the storyboard entry point?

If I'm using a storyboard and the entry point is for viewController1.

Is there a way to have the App Delegate run the conditional and then chose the storyboard entry point - either viewController1 or viewController2?

I want to make a choice from App Delegate on where location services are turned on or not and then do something like:

   (![CLLocationManager locationServicesEnabled]) 
   {

        self.viewController = [[viewController1 alloc] init];

        NSLog(@"vc is viewController2 from app del. loc svcs off");

    }
    else if ([CLLocationManager locationServicesEnabled])  
    {
        // alert location services denied

        self.viewController = [[viewController2 alloc] init];

        NSLog(@"vc is viewController2 from app del. loc svcs on");

        NSLog(@"core location is on");

    }

Yes You can do that.

write your condition in following method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Do something like this

if(Con1)
{
   window.rootViewController = [window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"rootViewController1"];
}
else
{
    window.rootViewController = [window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"rootViewController2"];
}

You can set VC1 as your initial view controller (in general) and if you instead want to present VC2 as your initial controller, do this in the appDelegate :

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
[self.window setRootViewController:[storyboard instantiateViewControllerWithIdentifier:@"VC2"]];
[self.window makeKeyAndVisible];

If you don't explicitly do makeKeyAndVisible, iOS does it automatically with the initial view controller from the storyboard

    NSString *identifier;

    (![CLLocationManager locationServicesEnabled]) {

          identifier = @"UNIQUE_ID_OF_VIEW_CONTROLLER1";
    }

    else if ([CLLocationManager locationServicesEnabled])

    {
          identifier = @"UNIQUE_ID_OF_VIEW_CONTROLLER2";

    }   

    UIViewController *firstView = [storyboard instantiateViewControllerWithIdentifier:identifier];

    // NOW SET IT ROOT VIEW CONTROLLER OF THE APP 
    [self.window setRootViewController:firstView];

    [self.window makeKeyAndVisible];

    return YES;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM