The development of IOS applications usually adopts the Model-View-controller architecture. The view is usually the uiview and its subclass in the cocoa framework.
In an iOS app, view is usually a large memory user, so it is especially important to effectively manage the view memory. In uiviewcontroller, an important attribute is view, which is the superview of all views in the Controller. Its life cycle is as follows:
Generation and initialization:
<textarea readonly name="code" class="plain">-(Void) loadview <br/>{< br/> uiview * view = [[uiview alloc] initwithmainframe] auotorelease; // method in the category. <Br/>... <Br/> uiview * subview = [[[uiview alloc] initwithframe: aframe] autorelease]; <br/>... // Modify the subview. <Br/> self. subview = subview; // if necessary. <Br/> [view addsubview: Self. subview]; <br/> self. view = view; <br/>}</textarea>
Release: <textarea readonly name="code" class="plain">-(Void) viewdidunload <br/>{< br/>... <br/> self. subview = nil; // it can be considered as the standard practice of releasing attributes in objc. <Br/> [Super viewdidunload]; // The order must be placed at the end. <Br/>}</textarea>
Such a structure is very simple and clear. Each view is responsible for the memory management and display of the view and all its subviews (composite pattern ). Note that the sentence self. subview = nil should be placed in viewdidunload rather than dealloc. IOS does not support the virtual memory mechanism. When the IOS memory is insufficient, the currently unavailable controllers will execute viewdidunload to release some temporarily unavailable memory space. Let's talk about proxy pattern related to uiview. In the MVC Architecture, view is only responsible for displaying and interacting media, but not for interactive response,ProgramTherefore, the interaction response and program logic must be separated from the View class to conform to the encoding principles of high cohesion and low coupling, the significance of proxy patter is the embodiment of the "delay" method. Objc provides language-level support for proxy pattern, so it is easy to implement-usually declare the protocol in the header file and implement the Protocol in the controller, set the view's delegate attribute (or datasource, etc.) to the Controller. In many cases (the proxy method has a return value or multiple parameters), you cannot simply use [self. delegate implements mselector:] To Call proxy methods. The nsinvoke class is required. Detailed usage, skipped.
NOTE 1: The loadview method in the uiview subclass cannot inherit the loadview method of the uiview, which must be rewritten. It is recommended by the official team for unknown reasons. NOTE 2: objc refers to the Smalltalk genre, which is called the OO term "method" as "message", meaning that it is a runtime binding. The article involves a lot of OOD, therefore, the OO term "method" is still used ".