Instantiating a View Controller programmatically with Storyboard from AppDelegate

前端 未结 4 907
天命终不由人
天命终不由人 2020-12-30 07:58

I\'m busy building an app - that when launched for the first time it asks the user to do two things:

  1. Select a Country
  2. Accept T&Cs
<
相关标签:
4条回答
  • 2020-12-30 08:36

    There are two things you need to do:

    1. In Storyboard you have mentioned the controller name (eg LoginView) and enable use storyboard ID
    2. Then you have user the below line

      loginView = [storyboard instantiateViewControllerWithIdentifier:@"LoginView"];
      [(UINavigationController*)self.window.rootViewController pushViewController:loginView animated:NO];
      

    Hope this helps. Let me know if you're still having the issue.

    0 讨论(0)
  • 2020-12-30 08:38
    [self performSegueWithIdentifier:@"buyListSegue" sender:sender];
    
    0 讨论(0)
  • 2020-12-30 08:40

    You're expecting the self.window.rootViewController to be a UINavigationController but it's a UIViewController. This means that the outermost view controller in your storyboard is of the wrong type.

    A better way to obtain the UINavigationController (Which should work in this case) is to use the property self.navigationController on any UIViewController.

    From what I understand you want to present a view the first time the user runs to have the user pick some stuff. What you should then do is present a modal view controller, like this:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //isFirstRun is some boolean you use to determine if it's the first run
    if(isFirstRun){
        BBCounterySettingsViewController *controller = (BBCounterySettingsViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: @"CountrySettings"];
        [self.window.rootViewController presentViewController: controller animated:YES completion:nil];
    }
    
    0 讨论(0)
  • 2020-12-30 08:57

    I think the controller identifier "CountrySettings" is set to the wrong controller. You're not getting a NavigationController where you can call pushViewController...

    0 讨论(0)
提交回复
热议问题