Modal View Controllers


Modal View Controllers


About Modal View Controllers





1. When you present a modal view controller, the system creates a parent-child 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 modalViewController property to point to its presented (child) view controller. Similarly, the presented view controller updates its parentViewController property to point back to the view controller that presented it.




2. A modal view controller that presents another modal view controller has valid objects in both its  parentViewController  and  modalViewController  properties. If the user cancels the current operation, you could remove all objects in the chain by dismissing the first modally presented view controller. In other words, dismissing a modal view controller dismisses not only that view controller but any view controllers it presented modally.



onfiguring the Presentation Style for Modal Views





For iPad applications, you can present content modally using several different styles.View controllers use the value in their modalPresentationStyle property to determine their appearance when presented modally. 


Presenting a View Controller Modally



To present a view controller modally, 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 where appropriate. (The delegate is used primarily by system view controllers to notify your code when the view controller is ready to be dismissed. )
  4. Call the presentModalViewController:animated: method of the current view controller, passing in the view controller you want to present modally.



[plain]  view plain copy print ?
  1. - (void)add:(id)sender {  
  2.    // Create the root view controller for the navigation controller  
  3.    // The new view controller configures a Cancel and Done button for the  
  4.    // navigation bar.  
  5.    RecipeAddViewController *addController = [[RecipeAddViewController alloc]  
  6.                        initWithNibName:@"RecipeAddView" bundle:nil];  
  7.    
  8.    // Configure the RecipeAddViewController. In this case, it reports any  
  9.    // changes to a custom delegate object.  
  10.    addController.delegate = self;  
  11.    
  12.    // Create the navigation controller and present it modally.  
  13.    UINavigationController *navigationController = [[UINavigationController alloc]  
  14.                              initWithRootViewController:addController];  
  15.    [self presentModalViewController:navigationController animated:YES];  
  16.    
  17.    // The navigation controller is now owned by the current view controller  
  18.    // and the root view controller is owned by the navigation controller,  
  19.    // so both objects should be released to prevent over-retention.  
  20.    [navigationController release];  
  21.    [addController release];  


Dismissing a Modal View Controller


1. When it comes time to dismiss a modal view controller, the preferred approach is to let the parent view controller do the dismissing.

2. In a delegate-based model, the view controller being presented modally must define a protocol for its delegate to implement. The protocol defines methods that are called by the modal view controller in response to specific actions. The delegate is then responsible for implementing these methods and providing an appropriate response. 

This use of delegation to manage interactions with a modal view controller has some key advantages over other techniques:

  • The delegate object has the opportunity to validate or incorporate changes from the modal view controller before that view controller is dismissed.
  • The use of a delegate promotes better encapsulation because the modal view controller does not have to know anything about the parent object that presented it. This enables you to reuse that modal view controller in other parts of your application.


[plain]  view plain copy print ?
  1. @protocol RecipeAddDelegate <NSObject>  
  2. // recipe == nil on cancel  
  3. - (void)recipeAddViewController:(RecipeAddViewController *)recipeAddViewController  
  4.                    didAddRecipe:(MyRecipe *)recipe;  
  5. @end  

[plain]  view plain copy print ?
  1. - (void)recipeAddViewController:(RecipeAddViewController *)recipeAddViewController  
  2.                    didAddRecipe:(Recipe *)recipe {  
  3.    if (recipe) {  
  4.       // Add the recipe to the recipes controller.  
  5.       int recipeCount = [recipesController countOfRecipes];  
  6.       UITableView *tableView = [self tableView];  
  7.       [recipesController insertObject:recipe inRecipesAtIndex:recipeCount];  
  8.    
  9.       [tableView reloadData];  
  10.    }  
  11.    [self dismissModalViewControllerAnimated:YES];  
  12. }  

Presenting Standard System Modal View Controllers


转自:http://blog.csdn.net/nerohoop/article/details/7034348
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值