有没有办法让应用程序代理运行条件,然后选择情节提要入口点



如果我使用的是故事板,并且入口点是viewController1。

有没有办法让应用程序代理运行条件,然后选择情节提要入口点-viewController1或viewController2?

我想从应用程序代表那里选择是否打开定位服务,然后做一些类似的事情:

   (![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");
    }

是的,你可以做到。

用以下方法写下你的情况:

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

做一些类似的事情

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

您可以将VC1设置为初始视图控制器(通常),如果您想将VC2显示为初始控制器,请在appDelegate:中执行此操作

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

如果你没有明确地进行makeKeyAndVisible,iOS会自动使用故事板的初始视图控制器进行操作

    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;

最新更新