(1) There are a lot of settings for editing and ending editing for text boxes that require a text box protocol, Uitextfielddelegate: The <UITextFieldDelegate> protocol is first added to the AppDelegate.h, Then you can use it in the VIEWCONTROLLER.M. For example, in this case, we set the proxy for T1, the self, the view controller class, so the methods used in this class will affect T1, that is, you can set the T1. (The last sentence is speculation, follow-up study to see if this is the case).
(2) Of course, the actual number of functions really do not mean, is not allowed to edit and end the edit, if you really "edit" and "end edit" can give it to set an action, and whether to allow cleanup.
(3) One of the most important is to set the Textfieldshouldreturn to hide the keyboard after return, this method is [T1 Resignfirstresponder];
(4) Immediately after, we implemented a keyboard that appears and hides, and if there is a view obscured by it, we also adjust these obscured view locations (such as the button for this example). Here to use a notice, of course, we are also familiar with the UIView inside the Animatewithduration animation.
#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.clearbuttonmode=uitextfieldviewmodeunlessediting; Set the proxy for T1 to self, that is, we are now writing the class (file) t1.delegate=self that is being used; After we add a Uicontrol object, covering the entire IPhone6 simulation screen, setting a freshman point doesn't matter Uicontrol *control1=[[uicontrol Alloc]init]; Control1.frame=cgrectmake (0, 0, 500, 800); [Self.view Addsubview:control1]; Control1.backgroundcolor=[uicolor Redcolor]; We found that although control1 is post-loaded, it does not seem to block the text boxes that were loaded before//And even if I get the control1 to the top, it still does not obscure (in the lower version is the need to adjust the order of occlusion), strange, intelligent? [Self.view Bringsubviewtofront:control1]; [Control1 addtarget:self Action: @selector (Hidekeyboard) Forcontrolevents:uicoNtroleventtouchupinside]; [Self.view ADDSUBVIEW:T1]; [Self.view Addsubview:t2]; Create a button, with the keyboard up and down, so that the keyboard is not obscured//btn1 need to be a global variable, because in the following need to call it to change its position, move up and down Btn1=[uibutton buttonwithtype: Uibuttontyperoundedrect]; Btn1.frame=cgrectmake (10, 500, 300, 30); Btn1.backgroundcolor=[uicolor Whitecolor]; [Btn1 settitle:@ "point me," forstate:uicontrolstatenormal]; [Self.view ADDSUBVIEW:BTN1]; You need to set up a notification, get the keyboard display and hide the message from the system, and then make a location change operation//Here the system comes with uikeyboardwillshownotification [[Nsnotificationcenter Defaultcenter]addobserver:self selector: @selector (keyboardshow) name:uikeyboardwillshownotification Object:nil]; [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (keyboardhide) Name: Uikeyboardwillhidenotification Object:nil]; [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} Click Control1 (relative to the user is click elsewhere), keyboard hiding-(void) hidekeyboard{[T1 Resignfirstresponder];} When the keyboard is displayed, the button runs up and will not be obscured-(void) keyboardshow{ Btn1.frame=cgrectmake (10, 180, 300, 30);} Keyboard hiding is, button down back to the original position//This inside try an animation, and no animation, directly determine 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 is not editable, the default is yes-(BOOL) textfieldshouldbeginediting: (Uitextfield *) textfield{return YES;} Method already edited-(void) textfielddidbeginediting: (Uitextfield *) textfield{NSLog (@ "already edited");} Whether you can end the edit, if it is no, then you point to other places, the cursor is still here to beat edit, the default is yes-(BOOL) textfieldshouldendediting: (Uitextfield *) textfield{ return YES;} Method at end of edit-(void) textfielddidendediting: (Uitextfield *) textfield{NSLog (@ "edit ended");} Whether it can be cleared. If it is no, we end the edit although there is a fork x, but we can manually clear, that is, although set to No, but also set its text is empty, the following//difference is that the latter, we edit the other text box when we click on it to clear, this time the cursor is still the original text box, Does not jump to this cleared text box-(BOOL) Textfieldshouldclear: (Uitextfield *) textfield{[email protected] ""; return NO;} If you can click the return key, no setting seems to be temporarily unable to see the effect. We used a lot more function is to press RETURN after the hidden keyboard-(BOOL) Textfieldshouldreturn: (Uitextfield *) TEXTField{//Cancel the first responder, is the end of the input disease hidden keyboard [T1 resignfirstresponder]; return YES;} @end
Cut a Picture:
"iOS Dev-16" Uitextfield protocol usage, keyboard hiding, and view adjust position up and down as the keyboard appears hidden