IOS Removal method sample from parent view

Source: Internet
Author: User

View structure

In iOS applications, the structure of a view is a tree-type data structure that controls the view display, which has a good advantage:

Hierarchical relationships are distinct and facilitate the delivery of events. Starting from the root node, extending down through the leaf node, the previous node of the same branch is the Superview of the next node, and the next node is the subview of the previous node. Each application has a main window, and this window is the root node.

Removefromsuperview

Each view is directly related to the structure of the views and the response chain, but this article is not intended to focus on these two aspects, mainly about removefromsuperview methods. Removing the current view from its parent view requires calling the Removefromsuperview method. Here is Apple's official definition of the API:

Unlinks the receiver from its superview and its windows, and removes it from the responder chain.

Remove the current view from its parent view and window, and also remove it from the responder chain that responds to the event action.

Removefromsuperview is the operation of a View node deletion, which is equivalent to finding the node in the tree structure, deleting the node and its child nodes from the tree, and not just deleting the node itself. At the same time, another operation is to remove the object from the responder chain.

Executes the Removefromsuperview method, except that the view is not displayed on the screen and does not remove the view from memory. So if we need to use this view, we don't need to create it again, we can just addsubview it.

For this API, Apple did not give too much explanation, but simply described the API, and explained the API's point of attention.

Memory traps

If so, there is an answer that is not quite the same as what we had before. What exactly is the problem, or do we need to write code validation, so I based on the test environment described above, wrote some about the view of the test code.


UIView *view = [[UIView alloc] Initwithframe:[uiscreen mainscreen].bounds];
[Self.view Addsubview:view];
[View release];
[View Removefromsuperview];
Call the Remove method multiple times
[View Removefromsuperview];

My test found that the reference count did not increase after the call to the Removefromsuperview method, and then it was release after the call was finished. The increase in the reference count we saw earlier was due to the hidden operation of the system. Retaincount was often found to be inaccurate during the MRC period, mainly because of the iOS API reference, or the automatic release pool, so Retaincount is not a reliable reference.

Therefore, if you call multiple release, it will still crash, always believe that iOS's MRC memory management principles, this is reliable. You can call the Removefromsuperview method multiple times, and after the parent view has been removed, the other unwanted calls do not change any reference counts. For Addsubview: The method is the same

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.