"Re-understanding of the iOS view"

Source: Internet
Author: User

Re-understanding of the view

First of all, in an application, there is only a single Window object, that is, windows, and then the various views of the added process is to add each view on the top of the window, just like on a blackboard with a variety of sticky notes on the same paper, but some of the sticky note paper is smart note paper, Can be connected with a mobile phone, the mobile phone processing some more complex features, the phone is the view controller. If you want to give another example, then the view is like the rich people's gentleman, all you can see the good-looking and cool is the face of the young master, and the view controller is like a master's housekeeper, help young master handle all kinds of things. Each view controller can have a master, many other small views. The main view is the controller's view property.

However, some view controllers are special, such as the Navigation view Controller Uinavigationcontroller, the controller will control the appearance of many other controllers, like a manager home.

The general understanding is positioned later, to see which views of the function will be called (the following are written in order, the actual view is generated in the order of invocation in the following order).

1  2 3 #pragmaMark-viewloading4 5- (void) Viewdidload6 7 {8 9 [Super Viewdidload];Ten  One //additional setup after loading the view, typically from a nib. A  -LOG (@"Hello"); -  the logsize (self.view.frame.size); -  - Logrect (self.view.frame); -  + Logpoint (self.view.frame.origin); -  + } A  at-(void) Viewwillappear: (BOOL) Animated -  - { -  - [Super viewwillappear:animated]; -  in } -  to //IOS6, which is called when the main view is changed, such as screen rotation +  --(void) Viewwilllayoutsubviews the  * { $ Panax Notoginseng      -  the } +  A-(void) Viewdidlayoutsubviews the  + { -  $      $  - } -  the-(void) Viewdidappear: (BOOL) Animated - Wuyi { the  - [Super viewdidappear:animated]; Wu  - } About  $ #pragmaMark Viewdestroy -  --(void) Viewwilldisappear: (BOOL) Animated -  A { +  the [Super viewwilldisappear:animated]; -  $ } the  the-(void) Viewdiddisappear: (BOOL) Animated the  the { -  in [Super viewdiddisappear:animated]; the  the   About  the}

In terms of the rotation of the view, the following functions are called (in the following order)

Determine if you can rotate

-(BOOL) shouldautorotate

{

return YES;

}

Return can support the direction of rotation,ipad default is to support all directions,iphone is in addition to upside down in other directions.

-(Nsuinteger) supportedinterfaceorientations

{

return uiinterfaceorientationmaskall;

}

Before the rotation, the layout does not change when the call

-(void) Willrotatetointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: ( Nstimeinterval) Duration

{

LOG (@ " not yet started ");

logpoint(selflabfortest. Frame. Origin);

}

-(void) Viewwilllayoutsubviews

{

LOG (@ " to change ");

logpoint(selflabfortest. Frame. Origin);

}

-(void) Viewdidlayoutsubviews

{

LOG (@ " changed ");

logpoint(selflabfortest. Frame. Origin);

}

All the post-rotation layouts are over.

-(void) Willanimaterotationtointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: (nstimeinterval) duration

{

LOG(@ " post-rotation layout has started");

logpoint(selflabfortest. Frame. Origin);

}

The layout of the rotation is all over.

-(void) Didrotatefrominterfaceorientation: (uiinterfaceorientation) frominterfaceorientation

{

LOG (@ " End all ");

logpoint(selflabfortest. Frame. Origin);

}

However, it is important to note that the method of two sunviews before rotation is called and does not know why, and it is called two times when a view appears. At the same time, the view has been adjusted over the last method call, and has not been adjusted before. So you can change the current view in the method before the last method to the IB's own set as a landscape view, this can achieve more complex vertical and horizontal screen switching, such as the portrait screen when there are two labels, horizontal screen when there are three such effects (born?). ), just like the IPhone's own calculator. The effect is like this:

The way to do this is simple, just write the added code in the method before the last method: (Landscape in the code below is a landscape view created in IB, which is set on the right)

All the post-rotation layouts are over.

-(void) Willanimaterotationtointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: (nstimeinterval) duration

{

LOG(@ " post-rotation layout has started");

logpoint(selflabfortest. Frame. Origin);

if (tointerfaceorientation==uiinterfaceorientationlandscapeleft| | Tointerfaceorientation==uiinterfaceorientationlandscaperight) {

[self. View addsubview: Landscapeview];

}

else{

[Landscapeview Removefromsuperview];

}

}

The actual writing here also found a small detail, that is, if the target setting in the project does not put Uidevice oritation plus upsidedown (upside-down direction), even in the All-directional support is returned in the Supportedinterfaceorientations method, and it is not used, upside down or not. It must be selected before it can be used, because Interfaceorientation is from decive Orientation. At the same time, the view controller's main view must be removed from the view controller before being replaced, otherwise it will be an error, so the Addsubview method is used instead of the direct substitution.

In addition, not only the view can be multi-layered development, the View controller can also be multi-layered development, and the controller has a callback function, so it is more convenient to develop. Typically this multi-level view develops a view controller for building its own container class. What is a view controller for a container class? is a controller that can load other controllers like Navigationcontroller. If sometimes the system comes with a convenient Navigation or Tabber controller is not too flexible to correspond to the current development requirements, you can write a controller.

The way to write custom multilayer controllers is simple, first as a controller that controls other view controllers (a bit of a mouthful), and you have to be able to get these view controllers at any time, and each view controller has its own way to help developers do this:

FIRSTVC=[self. Storyboard instantiateviewcontrollerwithidentifier:@ "one"];

[self addchildviewcontroller: FIRSTVC];

The second method above puts a view controller into the Childcontrollers property of our master controller, which is an array-type property that can hold the other view controllers in the master controller's charge.

Then set up a sub-view of the view in the current view where you want to show the other view controllers (the white one is, for convenience), then set a view to display first:

[self. Childview addsubview: FIRSTVC. View];

self. CURRENTVC=FIRSTVC;

Then the action of the button is set to:

If (self. CURRENTVC==firstvc) {

return;

}

[self transitionfromviewcontroller:self. CURRENTVC toviewcontroller: FIRSTVC Duration: options:0 Animations: Nil completion: ^ (BOOL finished) {

self. CURRENTVC=FIRSTVC;

}];

The effect of the second method is that the current view belongs to the other view controller of the sub-view to replace, into another view controller view, the reason is to judge, because the view load takes time, if not to judge when the user clicks the button will be an error.

"Re-understanding of the iOS view"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.