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