[iOS] Why not call Self.view in the Init initialization method

Source: Internet
Author: User
<span id="Label3"></p><p><p>First of All. If you call self.view, the View's getter method is called, and when the view is empty, the system automatically creates a view for you and then triggers the viewdidload method. so this time, If you have an array initialization in your init method. but when you're not there, and you assign the array directly, the value is assigned to an array that doesn't exist. this makes it easy to make mistakes. so try not to write the visual Control's statements in the Init method.</p></p><p><p></p></p><p><p>The life cycle of the attached 1:uiviewcontroller:</p></p><p><p></p></p><p><p>When a view controller is created and displayed on the Screen. Order of execution of code<br>1, alloc Create objects, allocate space<br>2. Init (initwithnibname) initializes the object, initializes the data<br>3, Loadview from the nib load view, Usually this step does not need to interfere. Unless you're not using the Xib file to create a view<br>4, viewdidload loading complete, can be customized data and dynamic creation of other controls<br>5, viewwillappear View will appear before the screen, the view will be displayed on the screen immediately<br>6, viewdidappear View has been rendered on the screen completed</p></p><p><p>When a view is removed from the screen and the execution order is destroyed, the order is almost the opposite of the above<br>1. Viewwilldisappear view will be removed from the screen before execution<br>2, viewdiddisappear View has been removed from the screen, the user can not see this view<br>3. Dealloc view is destroyed, you need to release the objects you created in Init and viewdidload</p></p><p><p>About Viewdidunload: in the event of a memory warning, if this view is not the view being displayed on the current screen, viewdidunload will be executed and all child views of this view will be destroyed to free up memory, when developers need to manually viewload, The objects created in the Viewdidload free memory. Because when the view is displayed again on the screen, viewload and Viewdidload are called again to construct the view Again.</p></p>When we create an object of the Uiviewcontroller class, it is common for the system to generate several default methods, most of which are related to the invocation of the view, but the order in which these methods are called when the view is called, needs to be sorted Out.<p><p>Usually the above methods include the following, which are methods of the Uiviewcontroller class:</p></p><p><p>-(void) viewdidload;</p></p><p><p>-(void) viewdidunload;</p></p><p><p>-(void) viewwillappear: (BOOL) animated;</p></p><p><p>-(void) viewdidappear: (BOOL) animated;</p></p><p><p>-(void) viewwilldisappear: (BOOL) animated;</p></p><p><p>-(void) viewdiddisappear: (BOOL) animated;</p></p><p><p></p></p><p><p>The following describes the order in which the app is called at Runtime.</p></p><p><p>1)-(void) viewdidload;</p></p><p><p>An app loads the view into memory first by invoking the Loadview method or by loading the initial interface created in ib. The Viewdidload method is then called to make further settings. In general, we have a lot of content of initial data loading, initial setting and so on, this method is implemented in this way, so this method is a very common, very important method.</p></p><p><p>note, however, that this method will only be called once when the app starts to load and will not be called again, so it can only be used for initial setup.</p></p><p><p>2)-(void) viewdidunload;</p></p><p><p>In the case of sufficient memory, the view of the software is usually kept in memory, but if there is not enough memory, some viewcontroller that are not being displayed will receive a warning of insufficient memory and then release the view they own to achieve the purpose of freeing the Memory. But the system only frees up memory and does not release ownership of the object, so usually we need to take ownership of objects that do not need to be kept in memory, that is, to set their pointer to Nil.</p></p><p><p>This method is not usually called when the view is transformed, but only when the system exits or a memory warning is Received. But since we need to make sure we can react to it when we receive a memory warning, this approach usually needs to be implemented.</p></p><p><p>In addition, even after the home key is pressed on the device, the system does not necessarily call this method, because after IOS4, the system allows the app to hang in the background and continue to be stuck in memory, so Viewcontroller does not call this method to clear the Memory.</p></p><p><p>3)-(void) viewwillappear: (BOOL) animated;</p></p><p><p>After the system loads all the data, the view is displayed on the screen, and this method is called First. We usually use this method to make further settings for the view that will be Displayed. For example, We can use this method to set how the device will be displayed in different directions.</p></p><p><p>On the other hand, when the app has multiple views, switching between views does not load the Viewdidload method again, so if the data needs to be updated when the view is transferred, it can only be implemented within this METHOD. So this method is also very common.</p></p><p><p>4)-(void) viewdidappear: (BOOL) animated;</p></p><p><p>sometimes, for some special reason, we cannot update the view in the Viewwillapper method. Then you can override this method to further set the view that is being Displayed.</p></p><p><p>5)-(void) viewwilldisappear: (BOOL) animated;</p></p><p><p>When the view is transformed, the current view is called when it is about to be removed or overwritten, and this method is invoked to handle and set up some aftercare.</p></p><p><p>Since after IOS4, the system allows the app to hang in the background, the system does not call this method after the home key is pressed, because the app itself, the view that the application displays, is still the view at the time of the hang, so this method is not Called.</p></p><p><p>6)-(void) viewdiddisappear: (BOOL) animated;</p></p><p><p>We can override this method and do some other work on the view that has disappeared, or is overwritten, or has been Hidden.</p></p><p><p></p></p><p><p></p></p><p><p>The flowchart of the above method can be expressed simply as Follows:</p></p><p><p></p></p><p><p>Run app-> load view, call the Viewdidload method, call the Viewwillappear method, call the Viewdidappear method, and run normally</p></p><p>aaaaaaaa                                                                                              a                                                                                 |  </p><p><p> aaaaaaaa                                                                                               |                                                                                 |  </p> </p><p><p> aaaaaaaa                                                                                               |  Loading New view                                                             |  </p></p><p>aaaaaaaa                                                                                               |                                                                                 |  </p><p><p> aaaaaaaa                                                                                               |                                                                                 v </p></p><p><p>Release object ownership <-call viewdidunload <-receive memory warning <-call viewdiddisappear <-call viewwilldisappear <-app need to call another view</p></p><p><p>iOS Program start execution order</p></p><p><p>http://www.yifeiyang.net/iphone-developer-advanced-3-iphone-application-startup-process/</p></p>The difference between IOS development Loadview and Viewdidload<p><p>These two methods are essential to the development of the IPHONE. They can all be used to initialize some content when the view is Loaded. But what difference do they have?</p></p><p><p>Viewdidload This method is called only when view is initialized from the nib File.</p></p><p><p>Loadview This method is called when the Controller's view is Nil. This method is used when creating a view programmatically. Such as:</p></p><p><p><span class="cnblogs_code">-( <span style="color: #0000ff;">void</span> <span style="color: #000000;"> ) loadview {</span> </span></p></p><p><p><span class="cnblogs_code"><span style="color: #000000;">UIView </span>*view =<span style="color: #000000;"> [[UIView alloc] initwithframe:[uiscreen mainscreen]. applicationframe];</span></span></p></p><p><p><span class="cnblogs_code"><span style="color: #000000;">[view setbackgroundcolor:_color];</span></span></p></p><p><p><span class="cnblogs_code"><span style="color: #000000;">Self.view </span>=<span style="color: #000000;"> view;</span></span></p></p><p><p><span class="cnblogs_code"><span style="color: #000000;">[view release];</span></span></p></p><p><p><span class="cnblogs_code"><span style="color: #000000;">}</span></span></p></p><p><p></p></p><p><p>You implement the Loadview method in the controller, then you may be called by the memory management control at some point when the application is Running. If the device is low on memory, The view controller receives a didreceivememorywarning Message. The default implementation is to check whether the current Controller's view is in Use. If its view is not inside the view hierarchy currently in use and your controller implements the Loadview method, the view will be release and the Loadview method will be re-used to create a new VIEW.</p></p><p><p>[iOS] Why not call Self.view in the Init initialization method</p></p></span>

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.