Programmatically build / navigate a Navigation Controller

后端 未结 2 1120
借酒劲吻你
借酒劲吻你 2020-12-16 08:32

Before:

My App is based on indepent view controllers. I can switch from one to another by replacing the root view controller on the application delegate:

         


        
相关标签:
2条回答
  • 2020-12-16 09:02

    In applicationDidFinishLoading in App delegate:

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    
    [window makeKeyAndVisible];
    [window addSubview:navController.view];
    

    That will instantiate the navigation controller and add it to the window as a view.

    Now, in your rootViewController class (lets say its called FirstViewController) you can do this:

    - (void)clickedAButton:(id)selector {
      SecondViewController *nextViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
      // and push it onto the 'navigation stack'
      [self.navigationController pushNavigationController:nextViewController animated:YES];
      // and release
      [nextViewController release];
    }
    

    And in your SecondViewController you can navigate back through the stack using:

    - (void)clickedAnotherButton:(id)selector {
      // goes back to the last view controller in the stack
      [self.navigationController popViewControllerAnimated:YES];
    }
    

    So for you it would go:

    Set up navigation controller in the app delegate with Brand as the root view controller User chooses their brand and you pushViewController:animated: the Model view controller. Then the user chooses their model and you pushViewController:animated: the Color view controller. Similarly the user chooses a color and you push the Pippo view controller. Now, if the user presses back (or you call popViewControllerAnimated:) it will go back to the Color view controller in the same state as when the user left it to go to the Pippo controller.

    0 讨论(0)
  • 2020-12-16 09:13

    Write the following code in AppDelegate.m Class

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
     {
       MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
        self.nav = [[UINavigationController alloc] initWithRootViewController:mainViewController];
        self.nav.navigationBarHidden = YES;
        [mainViewController release];
       [_window addSubview:nav.view];
       [_window makeKeyAndVisible];
     }
    
    0 讨论(0)
提交回复
热议问题