ArticleDirectory
- I. View Controller classes
- Ii. Custom uiviewcontroller
- 3. View Controller Lifecycle
- 4. Support page Rotation
- 5. View display related notifications
- 6. viewcontroller display (off) Other viewcontroller
- VII. edit mode of Controller
I. View Controller classes
Ii. Custom uiviewcontroller
1. Create
A) NIB file
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {self. window = [[[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds] autorelease]; // override point for customization after application launch. self. firstviewcontroller = [[[firstviewcontroller alloc] initwithnibname: @ "firstview" Bundle: Nil] autorelease]; self. window. rootviewcontroller = self. firstviewcontroller; self. firstviewcontroller. wantsfullscreenlayout = yes; [self. window makekeyandvisible]; return yes ;}
If you customize the Controller to implement initwithcoder: The method will be called. If not, the init.
After the call, the Framework automatically calls the awakefromnib method of objects (usually uiview) in the controller, so that objects can initialize itself.
B) manually create
You only need the alloc & init method.
2. Initialization
Generally, the loadview method is implemented in the Controller to manage the Controller content for the first time. It is worth noting that if you implement this method yourself, you do not need to call the super loadview method.
3. Set the size of the Root View Controller.
A) window size
B) whether the status bar exists
C) device direction (horizontal vertical)
D) wantsfullscreenlayout attribute of the Root View Controller. This attribute is used to set whether the status bar should contain 20 pixels. For example, if you set a button on the top of the view, if this is true, the button appears under the status bar, if it is false, it will automatically go down. It does not affect the hiding and disappearance of status bar. Only determines whether the status bar belongsProgramJust draw the area.
4. Layout
First, you can use the autoresizingmask settings of each view to automatically Layout
Order of viewcontroller layout changes
A) vew size change of View Controller
B)Call ControllerViewwilllayoutsubview.
C)Call ViewLayoutsubview
D)Call ControllerViewdidlayoutsubview.
3. View Controller Lifecycle
1. Initialization Method: init, initwithcoder;
2. Load the Controller's view: loadview: controller. view = nil; viewdidload: controller. view = view;
3. When didreceivememorywarning is received, viewwillunload and viewdidunload will be called;
4. dealloc: release resources (which can be ignored by arc)
4. Support page Rotation
1. Declare the supported Rotation Direction
Shouldautorotatetointerfaceorientation: determines the supported rotation direction (the horizontal direction of uiinterfaceorientationislandscape is two vertical directions: uiinterfaceorientationisportrait)
2. How to handle changes
A) The direction changes.
B) Call shouldautorotatetointerfaceorientation to view supported directions.
C) Call the willrotatetointerfaceorientation: Duration method of the controller.
D) trigger the automatic layout of the view (For details, refer to the 4th point in the second part: layout)
E) call the didrotatefrominterfaceorientation method.
3. Create different interfaces for each direction
Notification is used to notify different states.
@ Implementation portraitviewcontroller-(void) awakefromnib {region = no; [[uidevice currentdevice] region]; [[nsicationicationcenter defacenter center] addobserver: Self selector: @ selector (orientationchanged :) name: uideviceorientationdidchangenotification object: Nil];}-(void) orientationchanged :( nsnotification *) Notification {uideviceorientation Devi Ceorientation = [uidevice currentdevice]. Orientation; If (uideviceorientationislandscape (deviceorientation )&&! Attributes) {[self attributes: @ "displayalternateview" Sender: Self]; isshowinglandscapeview = yes;} else if (deviceorientation) & isshowinglandscapeview) {[self attributes: Yes completion: nil]; isshowinglandscapeview = no ;}}
4. Tips for processing Rotation
A. temporarily prohibit any event response
B) store the displayed map area (in the map application)
C) if the view layers on the interface are too complex and cause latency, we recommend that you first view them. The related methods are mentioned in other blog articles.
D) after rotation, reload is required for tableview to read data again.
E) Use the rotation notification to update application information.
5. View display related notifications
1. methods include viewwillappear: viewdidappear: viewwilldisappear: viewdidappear:
2. note that this is controller. view value or controller. view is set to nil for calling. Of course, this value may not be set directly, but may be displayed and removed through the Controller.
3. Check the reason why the view is removed. In the preceding four methods, call ismovingfromparentviewcontroller, ismovingtoparentviewcontroller, isbeingpresented, and isbeingdismissed.
6. viewcontroller display (off) Other viewcontroller
Before 5.0, the corresponding method is to use the Model View Controller series.After 5.0Added the presented and prensentingviewcontroller concepts. modalviewcontroller corresponds to the presentedviewcontroller after 5.0.
Fcdemoviewcontroller * controller = [[fcdemoviewcontroller alloc] initwithnibname: @ "fcdemoviewcontroller" Bundle: Nil]; controller. modaltransitionstyle = uimodaltransitionstylefliphorizontal; [self presentmodalviewcontroller: controller animated: Yes]; nslog (@ "% @", self. modalviewcontroller); // 5.0 [self presentviewcontroller: controller animated: Yes completion: Nil]; nslog (@ "% @", self. presentedviewcontroller); nslog (@ "% @", self. presentingviewcontroller );
The modalpresentationstyle attribute has several display forms under the iPad, corresponding to different display areas. The modelltransitionstyle attribute determines the transition form.
There are also corresponding methods to disable: dismissmodalviewcontrolleranimated or dismissviewcontrolleranimated: completion after ios5.0:
Other changes after 5.0:
The controller can define a set of sub-controllers so each controller can be a container controller.
Definespresentationcontext determines whether the controller of other controllers provides the context to the presentedviewcontroller (modalviewcontroller). If not, the value of the Controller at the upper level is queried.
For detailed operations, you can view the class reference.
Apple officially recommends protocol-based communication between controllers. First, declare a Protocol and implement the Protocol in the master controller. When other controllers are displayed, set delegate = self. in this way, if other controllers need to call back the presentingviewcontroller, they can be directly tuned back and forth using the delegate method. In this way, the reusability can be greatly enhanced. In addition, the displayed controller does not need to fully understand all information and attributes of its controller.
VII. edit mode of Controller
1. When the editing attribute of the controller is set, the setediting: animated attribute is automatically triggered. At this time, the edit button obtained through the mycontroller editbuttonitem will automatically change from Edit to done, which is usually used in the navigation Controller
For example, if you set the button in the upper-right corner to editbuttonCodeAs follows:
Myviewcontroller. navigationitem. rightbarbuttonitem = [myviewcontroller editbuttonitem];