Do not remove notifications in the Viewwilldisappear: method

Source: Internet
Author: User

All know Viewwillappear: The method is called when the controller's view is about to be displayed, while the Viewwilldisappear: method is called when the controller's view is about to be hidden.

This seems to be no problem, the keyboard display and hiding can also be heard. But a little more careful will find that Apple in the iOS7 added navigation controller side-by-side return function, the majority of apps now use this feature (some apps have rewritten their own return keys and forgot to turn on the left slide return gesture). So what is the problem with this feature?

The problem is:

When the trigger side-slip return will call the system's own Viewwilldisappear: method, in this method will listen to the keyboard display and hidden notifications removed, if the user cancels the side-by-side return (that is, back to the original state), then click on the interface TextField called out the keyboard, The keyboard display and hidden notifications are not sent because the notifications have been removed ...

Solution :

1. Put the registered monitor keyboard display and hidden code into the Viewwillappear: method

The Viewwillappear: method is called because the side-slip is canceled after the trigger side-slip is returned. Then if the side-slip return removes the notification, the notification will be added again when the slide is canceled.

2. Place the code to remove the notification in the Dealloc method

The Dealloc method is called when the controller is destroyed. Removing the notification at this time instead of Viewwilldisappear: Removing the method can effectively avoid the above problem. Now that the controller is destroyed, why do you keep the relevant notice? The removal of the removal.

The above gives two solutions, to say which is the best, it is certainly not the second kind.

I can give an example here. If we now have a scene with a label on the controller's view and a gesture on the label (the general gesture is added after the label is created), let's say I remove the gesture in the Viewwilldisappear: method, When the user slides back and then cancels the side-slip return, the previous gesture on the label is never added back (here the label is typically created in the Viewdidload method) because the Viewdidload method is not called again. The second method can effectively avoid this situation, so that I can avoid this "hand-cheap" arbitrary, is it perfect?

Summarized as follows:

1, iOS7 new added navigation controller slide gesture, when the trigger side-slip return, will call the system's Viewwilldisappear: Method, cancel the side-slip return and call Viewwillappear: method.

2, in the gesture and notification of a series of operations as far as possible in the Dealloc method to execute, add notification as far as possible in the viewdidload and other one-time methods to execute.

3, in Viewwillappear:, Viewwilldisappear:, Viewdidappear:, Viewdiddisappear: etc similar to this will be called several times in the system method to add code, must consider the business logic, So as not to cause unnecessary trouble.

Do not remove notifications in the Viewwilldisappear: method

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.