General rules for memory application and release of the Note IOS View Controller

Source: Internet
Author: User

Author: failingProgramMember-eight gods

Memory Management plays a very important role in IOS program development. For a program with poor processing performance, crash occurs on the actual device.

For output ports that are often used with retain in the View Controller, be sure to release them in dealloc.
The attribute declared by obj-C has the following features: Assign the new object to the current attribute and automatically release the old object in the attribute.

  1. In appdelegate. if there is a window output port in m, or a navigation View Controller or another view controller is added, execute the release method of these output ports in the dealloc method. instead of executing release in applicationdidfinishlaunching.
  2. In a separate custom View Controller, if the didreceivememorywarning method is executed when a notification of memory shortage is received, it uninstalls the current view form and executes the viewdidunload method, therefore, the output port defined in the square view is set to nil in this method. at the same time, execute the corresponding output port release method in the dealloc method of the view (this method is executed when the release of the View Controller is called.
     

    -(Void) Viewdidunload
    {
    Self.** =Nil;
    }

    -(Void) Dealloc
    {
    [**Release];
    [Supper dealloc];
    }

  3. The release method is automatically maintained by the memory pool of the system.
  4. If I add alloc TO THE appdelegate. M file and init A View Controller, and then add it to it through the initwithrootviewcontroller method of the navigation controller, then release the View Controller after adding the controller. Example:Mycontroller=[[***Alloc] initwithnibname:"***"Bundle: Nil];
    Uinavigationcontroller*Navi=[[Uinavigationcontroller alloc] initwithrootviewcontroller: mycontroller];
    [Mycontroller release];
    [Tabcontroller presentmodalviewcontroller: Navi animated: Yes];
    [Navi release];
  5. The variables are declared in the Custom View Controller, but the corresponding attributes are not defined. In this case, the object release rules are the same as the 1st.@ Interface *** : Uiviewcontroller
    {
    Uiview * _ View;
    }
    @ End

    @ Implementation***
    -(Void) Loadview
    {
    [Supper loadview];
    _ View=[[Uiview alloc] initwithframe: [self. View bounds];
    [Self. View addsubview: _ view];
    }

    -(Void) Dealloc
    {
    [_ View release];
    [Supper dealloc];
    }
    @ End

  6. In the basic development tutorial of iPhone 3, switch Code I think there is a wrong sequence in it. I checked it on Google and many people asked the same question.@ Implementation switchviewcontroller
    @ Synthesize blueviewcontroller;
    @ Synthesize yellowviewcontroller;

    - ( Void ) Viewdidload
    {
    Blueviewcontroller * Bluecontroller = [[Blueviewcontroller alloc] initwithnibname: @" Blueview " Bundle: Nil];
    Self. blueviewcontroller = Bluecontroller;
    [Self. View insertsubview: bluecontroller. View atindex: 0 ];
    [Bluecontroller release];
    }

    - (Ibaction) switchviews :( ID) sender
    {
    [Uiview beginanimations: @ "view flip" context: Nil];
    [Uiview setanimationduration: 1.25];
    [Uiview setanimationcurve: uiviewanimationcurveeaseinout];

    If (Self. yellowviewcontroller. View. superview = Nil) // this statement is the most interesting and the OBJ-C allows messages to nil.
    {
    If (Self. yellowviewcontroller = Nil)
    {
    Yellowviewcontroller * Yellowcontroller =
    [[Yellowviewcontroller alloc] initwithnibname: @" Yellowview "
    Bundle: Nil];
    Self. yellowviewcontroller = Yellowcontroller;
    [Yellowcontroller release];
    }
    [Uiview setanimationtransition: Uiviewanimationtransitionflipfromright Forview: Self. View cache: Yes];
    [Blueviewcontroller viewwillappear: Yes];
    [Yellowviewcontroller viewwilldisappear: Yes];
    [Blueviewcontroller. View removefromsuperview];
    [Self. View insertsubview: yellowviewcontroller. View atindex: 0 ];
    [Yellowviewcontroller viewdiddisappear: Yes];
    [Blueviewcontroller viewdidappear: Yes];
    }
    Else
    {
    If (Self. blueviewcontroller = Nil)
    {
    Blueviewcontroller * Bluecontroller =
    [[Blueviewcontroller alloc] initwithnibname: @" Blueview "
    Bundle: Nil];
    Self. blueviewcontroller = Bluecontroller;
    [Bluecontroller release];
    }
    // [Uiview setanimationtransition:
    // Uiviewanimationtransitionflipfromleft
    // Forview: Self. View cache: Yes];

    // [Yellowviewcontroller viewwillappear: Yes];
    // [Blueviewcontroller viewwilldisappear: Yes];
    [Yellowviewcontroller. View removefromsuperview];
    [Self. View insertsubview: blueviewcontroller. View atindex: 0 ];
    // [Blueviewcontroller viewdiddisappear: Yes];
    // [Yellowviewcontroller viewdidappear: Yes];
    }
    // [Uiview commitanimations];
    }

    -(Void) Didreceivememorywarning
    {
    [Super didreceivememorywarning];
    If(Self. blueviewcontroller. View. superview=Nil)
    Self. blueviewcontroller=Nil;
    Else
    Self. yellowviewcontroller=Nil;
    }

    -(Void) Dealloc
    {
    [Yellowviewcontroller release];
    [Blueviewcontroller release];
    [Super dealloc];
    }

    @ End

  7.  
    All are executed in dealloc. I have seen this in the WordPress iPhone client, but it seems that it is specially compatible with versions earlier than ios3. The viewdidunload method does not exist in versions earlier than 3.0, which is described in Apple's development manual.
     
     

    -(Void) Dealloc
    {
    [Myview release], myview=Nil;
    }

    Appendix.
    Python/users/yangiori/downloads/facebook-three20-309d34e/src/scripts/ttmodule. py-P/users/yangiori/documents/iphoneliaobao. xcodeproj three20/case sensitive...

Related Article

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.