Adding a gesture to a view does not retain the current view

Source: Internet
Author: User

After a gesture B is added to view a, view a Will rehold gesture B.

Therefore, if this gesture B is global, you should remove gesture B when releasing view A. Otherwise, gesture B cannot be release.

Of course, if we use gestures like the following, we do not need to remove this gesture when releasing view.

MVI = [[msviewalloc] initwithframe: cgrectmake (50,150,100,
(100)];

Uitapgesturerecognizer * tapgue = [[uitapgesturerecognizeralloc] initwithtarget: selfaction: @ selector (tapedcalled :)];

[MVI addgesturerecognizer: tapgue];

[Tapgue
Release];

This is because the gesture is only given to retain by the MVI. If the MVI is release along with the program, the gesture will also be release. Therefore, as long as the MVI is not released, this gesture will not be released, which must be remembered.

If multiple views share one gesture, you should be more aware of the retaincount of the gesture to prevent the memory from being released.

During the test, instruments was used for observation,

After an execution is added to a uiview, unless otherwise required

[MVI removegesturerecognizer: tapgue];

Operation, and there will be no memory leakage error,Estimated to beThis is because when a uiview such as MVI is dealloc, all the gesture objects added to it will be released. So there will be no memory leakage. In other words, it is of little significance to execute removegesturerecognizer if it is not particularly necessary.


To verify the above statement, it is easier to continue with a gesture class. Then I wrote a class to directly inherit uitapgesturerecognizer and only implement one method, that is, dealloc {

Nslog (@ "gesture dealloc ");

[Super dealloc];

}

Then add it to MVI, regardless of whether the execution is performed [MVI removegesturerecognizer: tapgue];
In this method, we found that the gesture method dealloc will be called when MVI release and dealloc are executed.

Therefore, it can be confirmed that when a class such as uiview is dealloc, the underlying layer executes an operation to remove all gestures. In this way, all the gestures added above will be released.

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.