IPhone development and learning notes 006 -- NSNotification

Source: Internet
Author: User

Recently, I saw a demo on the Internet, "add a hidden toolbar to the keyboard in iPhone development". After running the demo, I found that after clicking "hide the keyboard, the previous TableView cannot be restored to the display when the application is started, because when TextField gets the focus, a soft keyboard will pop up, and the corresponding TableView on the Interface needs to be adjusted through the program, modifying its Frame will move above. (Note: Five textfields are included in TableView ). However, after you click "Hide keyboard", TableView remains unchanged after its Frame is modified, but it is not restored to the original appearance when the application was started.

Tried

-(Void) textFieldDidEndEditing :( UITextField *) textField; the method adds the statement that modifies the TableView Frame to restore the display when it is started, but it is found that as long as the focus of TextField changes, this API is called. For example, you can jump from the first TextField to the second TextField, switch the focus from the first to the second, and then switch from the second to the third and fourth... This API is called as long as any TextField loses focus. As a result, every time a focus switch occurs, the page will first restore the display when the application is started, and then quickly become the modified Frame.

You have also tried to register the keyboard display and implicit notifications.
The same is found. when the focus is switched from one TextField to another, the system sends the UIKeyboardWillHideNotification and UIKeyboardWillShowNotification notifications one after another. You still cannot achieve the desired effect, that is, after you click "Hide keyboard, restore the TableView display on the main interface.

Then I thought of a custom notification to solve this problem:
In the Demo, there is a HiddenKeyBoard function. This is the selector that will be called after you click the "Hide keyboard" button. Then, add the send notification statement in it:


[[Nsnotifcencenterdefacenter] postNotificationName: @ "mytest" object: nil];


Then, register the notification in ViewController's viewWillAppear:

-(Void) viewWillAppear :( BOOL) animated {

[[Nsnotificationcenterdefacenter] addObserver: selfselector: @ selector (doAfterKeyboardbarHidden :) name: @ "mytest" object: nil];

 


[Super viewWillAppear: animated];

}

 


-(Void) viewWillDisappear :( BOOL) animated {

// [[Nsicationicationcenter defacenter center] removeObserver: self];

[[Nsnotifcencenterdefacenter] removeObserver: selfname: @ "mytest" object: nil];

[Super viewWillDisappear: animated];

}

 


-(Void) doAfterKeyboardbarHidden :( NSNotification *) notification {

// Id obj = [notification object]; // get the passed object

NSLog (@ "++ // ++ ");

Tableview. frame = CGRectMake (320,340 );

}

OK! The desired effect is achieved!
Paste two pictures: the first one is input. After the input is complete, click "Hide keyboard". The interface will be displayed again immediately, that is, the second figure:


From Code Heaven
 


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.