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.
To present a view controller modally, you must do the following:
- Create the view controller you want to present.
- Set the modalTransitionStyle property of the view controller to the desired value.
- 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. )
- Call the presentModalViewController:animated: method of the current view controller, passing in the view controller you want to present modally.
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.
转自:http://blog.csdn.net/nerohoop/article/details/7034348