[IOS development-16] UITextField protocol usage, keyboard hiding, and view with the appearance of the keyboard hidden and up and down adjust the position,-16 uitextfield

Source: Internet
Author: User
Tags uicontrol

[IOS development-16] UITextField protocol usage, keyboard hiding, and view with the appearance of the keyboard hidden and up and down adjust the position,-16 uitextfield

(1) There are many settings for text box editing and end editing that need to use the text box protocol, that is, UITextFieldDelegate: first in AppDelegate. add the <UITextFieldDelegate> protocol in h, and then you can go to ViewController. m. For example, we set proxy and proxy self for the text box object t1 in this example, that is, the Controller class of this view. Therefore, the methods used in this class will affect t1, that is, you can set t1. (The last few words are guesses. If so, continue to study later ).


(2) Of course, there are actually few functions that really don't mean much. It's just whether editing is allowed or not, if it is "edited" or "ended", you can set an operation for it, and set whether to allow clearing.


(3) The most important thing is to hide the keyboard after pressing Return in textFieldShouldReturn. This method is [t1 resignFirstResponder]; this is very practical.


(4) then, we implement a view that appears and hides along with the keyboard. If there is a view that is covered by it, we also adjust the positions of these hidden views (such as the buttons in this example ). A notification is used here. Of course, we are also familiar with the animateWithDuration animation in UIView.


# Import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController {UITextField * t1; UIButton * btn1;}-(void) viewDidLoad {// set two text boxes t1 = [[UITextField alloc] init]; UITextField * t2 = [[UITextField alloc] init]; t1.frame = CGRectMake (10, 30,300, 30 ); t2.frame = CGRectMake (10, 80,300, 30); t1.borderStyle = UITextBorderStyleRoundedRect; t2.borderStyle = UITextBorderStyleRoundedRect; t1.clea RButtonMode = UITextFieldViewModeUnlessEditing; // set the proxy of t1 to self, that is, we are currently writing this class (File) t1.delegate = self; // we will add a UIControl object, it's okay to overwrite the entire iPhone6 simulated screen and set it to a larger value. UIControl * control1 = [[UIControl alloc] init]; control1.frame = CGRectMake (0, 0,500,800); [self. view addSubview: control1]; control1.backgroundColor = [UIColor redColor]; // although control1 is post-loaded, but it seems that the previously loaded text box is not blocked, and even if I get control1 to the top, it is still not blocked (in earlier versions, it is blocked and the order needs to be adjusted), strange Is it smart? [Self. view bringSubviewToFront: control1]; [control1 addTarget: self action: @ selector (hideKeyboard) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: t1]; [self. view addSubview: t2]; // create a button that goes up and down with the keyboard so that it is not blocked by the keyboard. // btn1 must be a global variable, because you need to call it below to change its location, move up and down btn1 = [UIButton buttonWithType: bytes]; btn1.frame = CGRectMake (10,500,300, 30); btn1.backgroundColor = [UIColor whiteColo R]; [btn1 setTitle: @ "Click me" forState: UIControlStateNormal]; [self. view addSubview: btn1]; // you need to set a notification to obtain the system's keyboard display and hide messages, then perform the location change operation // The [[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (keyboardShow) name: UIKeyboardWillShowNotification object: nil] That comes with uikeydwillshownotification. [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyboardHide) name: UIKey BoardWillHideNotification object: nil]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} // when you click Control1 (as opposed to clicking somewhere else), the keyboard hides-(void) hideKeyboard {[t1 resignFirstResponder];} // when the keyboard displays, the button runs up, will not be overwritten-(void) keyboardShow {btn1.frame = CGRectMake (10,180,300, 30);} // hide the keyboard. Yes, click the button to return to the original position and try an animation, and no animation, directly determining the position is an effect, almost-(void) keyboardHide {[UIView animateWithDuration: 0. 25 animations: ^ {btn1.frame = CGRectMake (10,500,300, 30);} completion: ^ (BOOL finished) {}];} // set whether the text box can be edited, NO can be edited. The default value is YES-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField {return YES;} // method used when editing-(void) textFieldDidBeginEditing :( UITextField *) textField {NSLog (@ "edited") ;}// whether the editing can be completed. If NO is used, you can point it to another place and the cursor is still being edited, the default value is YES-(BOOL) textFieldShouldEndEditing :( UITextField *) textField {return YES ;} // Method at the end of editing-(void) textFieldDidEndEditing :( UITextField *) textField {NSLog (@ "edited ended");} // whether it can be cleared. If it is NO, although there is a cross-fork X at the end of editing, it cannot be cleared by clicking it. // However, We can manually clear it, that is, although it is set to NO, but set the text to null at the same time, as shown below // The difference is that, when we edit another text box, click it to clear it. At this time, the cursor is still in the original text box, will not jump to the cleared text box-(BOOL) textFieldShouldClear :( UITextField *) textField {t1.text = @ ""; return NO;} // can you click the return key, the NO setting seems to be unavailable for the moment. // We use the more frequently-used function to hide the keyboard after pressing return-(BOOL) textFieldShouldReturn :( UITextField *) textField {// cancel the first responder, end input disease hidden keyboard [t1 resignFirstResponder]; return YES ;}@ end

Screenshot:



How can I use UITextFieldDelegate to hide the keyboard?

Three steps are required:
1. Add the UITextFieldDelegate protocol to your controller class, for example:
@ Interface EditingPersonViewController: UIViewController
2. Add the textFieldShouldReturn Method to the implementation file as required by the Protocol, for example:
-(BOOL) textFieldShouldReturn :( UITextField *) textField {
[TextField resignFirstResponder];
Return YES ;}
3. Direct the delegate variable of the TextField control in the xib file to the Controller class that used the UITextFieldDelegate protocol before, and right-click the delegate IBOutlet variable of the TextField to the instance of the previous controller class.
You can also use code to specify the delegate variable of the relevant TextField.
-(Void) viewDidLoad {
[Super viewDidLoad];
ItemNameField. delegate = self;
PriceField. delegate = self;
} The third step is easy to ignore. Previously, because you forgot to specify the delegate variable, you can click the return key on the keyboard. The keyboard remains hidden.

In IOS software development, how does UITextField cancel the pop-up system keyboard?

Why must we focus? On your own keyboard, you can press the key to modify textField. text.

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.