How View Controllers Present Other View Controllers

童宏富
2023-12-01

When you present a modal view controller, the system creates a relationship between the view controller that did the presenting and the view controller that was presented. Specifically, the view controller that did the presenting updates its presentedViewController property to point to its presented view controller. Similarly, the presented view controller updates its presentingViewController property to point back to the view controller that presented it. Figure 10-1 shows the relationship between the view controller managing the main screen in the Calendar app and the presented view controller used to create new events.


Dismissing a view controller dismisses not only that view controller but also any view controllers it presented.


If you need to present a view controller programmatically, you must do the following:

  1. Create the view controller you want to present.
  2. Set the modalTransitionStyle property of the view controller to the desired value.
  3. Assign a delegate object to the view controller. Typically, the delegate is the presenting view controller. The delegate is used by the presented view controllers to notify the presenting view controller when it is ready to be dismissed. It may also communicate other information back to the delegate.
  4. Call the presentViewController:animated:completion: method of the current view controller, passing in the view controller you want to present.

- (void)add:(id)sender {


   // Create the root view controller for the navigation controller


   // The new view controller configures a Cancel and Done button for the


   // navigation bar.


   RecipeAddViewController *addController = [[RecipeAddViewController alloc]


                       init];


 


   // Configure the RecipeAddViewController. In this case, it reports any


   // changes to a custom delegate object.


   addController.delegate = self;



 // Create the navigation controller and present it.


   UINavigationController *navigationController = [[UINavigationController alloc]


                             initWithRootViewController:addController];


   [self presentViewController:navigationController animated:YES completion: nil];


}



 类似资料:

相关阅读

相关文章

相关问答