The life cycle problem of the controller object generated by iOS from storyboard

Source: Internet
Author: User

Suppose there are two controllers in the storyboard.

1.rootViewController: Root Controller

2.testViewController: Test controller, its storyboardid is set to test

Code in the root controller:

#import "RootViewController.h"#import "TestViewController.h"@interfaceTest1viewcontroller () {Testviewcontroller*test;//test Controller}@end@implementationRootviewcontroller ...//Root Controller Add button method, click on the button, will pop up the test controller interface-(Ibaction) buttontapped: (ID) sender{//generating an object using the storyboard methodTest=[self.storyboard Instantiateviewcontrollerwithidentifier:@"test1"]; //Show it dynamically[self presentviewcontroller:test animated:yes completion:null]; //print out the address of an object in memoryNSLog (@"%p", test);}//used to detect whether the test object was reclaimed by the system when the testing controller disappeared. -(Ibaction) Showtestretaincount: (ID) sender{NSLog (@"%d", [Test retaincount]);}@end

Code in the test controller:

@implementation Testviewcontroller // Add a Return button response method -(Ibaction) buttontapped: (ID) sender{    [self dismissviewcontrolleranimated: YES Completion:^{        // When the interface disappears, it prints its own reference count, while the object is not displayed on the interface, but is not yet reclaimed by the system, the reference count is not 0        NSLog (@ "%d", [self retaincount]);}    ];} @end

Conclusion:

Each time the memory address of the test controller object is displayed is different, this proves that a new object is generated each time.

When the test controller disappears and the reference count is displayed, the program crashes directly, proving that the memory occupied by the test object has been reclaimed by the system.

The test object is created by the method [Self.storyboard instantiateviewcontrollerwithidentifier:@ "test"].

In [Rootviewcontroller presentviewcontroller:test animated:yes Completion:null]; This method is shown.

Ultimately by [self dismissviewcontrolleranimated:yes completion:null]; This method ends the life cycle, and the test object is reclaimed by the system.

The life cycle problem of the controller object generated by iOS from storyboard

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.