This article is only for the help of many people. Suitable for beginners. here I introduce 3 kinds of methods to monitor
Uitextfield . And at last wrote a small demo to provide reference.
-------Please do not struggle with the name of the small series of rules, all only for common learning, common progress.
@property (Weak, nonatomic) Iboutlet Uitextfield *userid;
@property (Weak, nonatomic) Iboutlet Uitextfield *password;
@property (Weak, nonatomic) Iboutlet UIButton *loginbut;
several ways to listen to the contents of a text box: (for Login-to-search)
1. Agent (can only listen to the text box that has the proxy set not set the proxy text box does not listen)
<UITextFieldDelegate>
_userid.delegate=self (set proxy)
//Whether you can edit Yes to no
-(BOOL) textfieldshouldbeginediting: (Uitextfield *) textfield{
return YES;
}
//Start editing when the call
-(void) textfielddidbeginediting: (Uitextfield *) textfield{
NSLog (@ "Start editing");
}
//Whether to allow end edits (this is called first when the user ends a text box)
//If no means, this text box is always in the edit state (also called the first responder), even if you click another text box, it is not any effect
-(BOOL) textfieldshouldendediting: (Uitextfield *) textfield{
return YES;
}
//End of Edit call
-(void) textfielddidendediting: (Uitextfield *) textfield{
NSLog (@ "end edit");
}
//Whether the user is allowed to enter files
//The user will be called once each time a character is entered. Then judge the non-display in the text box
-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string{
//Print
nsstring *text=[nsstring stringwithformat:@ "%@%@", textfield.text,string];
NSLog (@ "------%@", text);
return YES;
}
//Allows you to clear what is entered in the current text box
-(BOOL) Textfieldshouldclear: (Uitextfield *) textfield{
return YES;
}
2. Notifications (all text boxes can be monitored)
/*
uitextfieldtextdidchangenotification//Notification of Change of text box
Object:_userid represents the _userid text box
Object:nil represents all text boxes
*/
[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (textchange) Name: Uitextfieldtextdidchangenotification Object:nil];
-(void) textchange{
NSLog (@ "-----change-----");
}
The notification to be created is to be
removed when the current pair is being destroyed: (otherwise an error may occur, bad memory access)
-(void) dealloc{
[[Nsnotificationcenter Defaultcenter] removeobserver:self];
}
3. Listener (AddTarget) ()
/*Events
: I'm going to lose this method when I'm in the event of something.
uicontroleventeditingchanged: When the text box is edited
*/
[_userid addtarget:self Action: @selector (TextChange) forcontrolevents:uicontroleventeditingchanged];
[_password addtarget:self Action: @selector (TextChange) forcontrolevents:uicontroleventeditingchanged];
}
//I set the 2 text box listener trigger method to TextChange to determine if the login button can be clicked
//Only 2 text boxes have values to click (here according to your needs)
-(void) textchange{
if (_userid.text.length && _password.text.length) {
_loginbut.enabled=yes;
}else{
_loginbut.enabled=no;
}
//This judgment can be optimized to one line:
_loginbut.enabled=_userid.text.length && _password.text.length;
NSLog (@ "Gaibia-----");
}
Demo
notifications are used with the agent:
If you have 5 text boxes or listen to multiple simultaneous listeners, I want to print out which text box (first responder) is in the editor and what the content is:
Code:
There is no need to drag the line (your storyboard to be associated with your Viewcontroller)
#import "ViewController.h"
@interface Viewcontroller () <UITextFieldDelegate>
//@property (nonatomic, strong) Nsarray *textfieldall;//store all TextField
@property (nonatomic, strong) Uitextfield *newtext;//get Uitextfield in edit state
@end
@implementation Viewcontroller
-(void) Viewdidload {
//Create an array store TextField
Nsmutablearray *FIELDSM = [Nsmutablearray array];
//No tow line
//This code is to get all the controls on the view, including the Label,textfield;
Nsarray *childarray=self.view.subviews;
//Loop through all the controls
For (UIView *child in Childarray) {
//Find out all the Textfieldall
if ([Child Iskindofclass:[uitextfield class]]) {
//Type conversion
Uitextfield *textfield= (Uitextfield *) child;
//Set up proxy
textfield.delegate=self;
//[FIELDSM Addobject:textfield];
}
}
//_TEXTFIELDALL=FIELDSM;
//Create a notification: Listen to every TextField
[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (textchange) Name: Uitextfieldtextdidchangenotification Object:nil];
}
-(void) textchange{
//print TextField and tag in edit state
NSLog (@ "%@-%ld", _newtext.text, (long) _newtext.tag);
}
-(void) textfielddidbeginediting: (Uitextfield *) textfield{
_newtext=[[uitextfield Alloc]init];
_newtext=textfield;
}
@end
last Run effect and print ——————————————————
iOS Listening TextField content changes