UITextfield代理方法传递
只要是输入内容的控件,都要先判断一下是不是为空。
enum {
NameFieldTag = 0,
EmailFieldTag,
DOBFieldTag,
SSNFieldTag
};
- (void)textFieldDidEndEditing:(UITextField *)textField {
switch (textField.tag) {
case NameFieldTag:
// do something with this text field
break;
case EmailFieldTag:
// do something with this text field
break;
// remainder of switch statement....
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([textField.text isEqualToString:@""]) return;
switch (textField.tag) {
case NameFieldTag:
[thePerson setObject:textField.text forKey:MyAppPersonNameKey];
break; case EmailFieldTag:
[thePerson setObject:textField.text forKey:MyAppPersonEmailKey];
break; case SSNFieldTag:
[thePerson setObject:textField.text forKey:MyAppPersonSSNKey];
break; default:
break;
} }
***************
这是textView
- (void)textViewDidEndEditing:(UITextView *)textView { NSString *theText = textView.text; if (![theText isEqualToString:@""]) {
[thePerson setObject:theText forKey:MyAppPersonNotesKey];
}
doneButton.enabled = NO; }
The sequence of messages that both text views and text fields send to their delegates is as follows:
1. Just before a text object becomes first responder—textFieldShouldBeginEditing: (text field) and
textViewShouldBeginEditing: (text view).The delegate can verify whether the text object should become first responder by returning YES (the default) or NO.
-
Just after a text object becomes first responder—textFieldDidBeginEditing: (text field) and textViewDidBeginEditing: (text view).
The delegate can respond to this message by updating state information or, for example, by showing an overlay view during the editing session.
-
During the editing session—various.
While the user enters and edits text, the text object invokes certain delegation methods (if implemented). For example, the delegate of a text view can receive a textViewDidChange: message when any text changes. The delegate of a text field can receive a textFieldShouldClear: message when the user taps the clear button of a text field; the delegate returns a Boolean value indicating whether the text should be cleared.
-
Just before a text object resigns first responder—textFieldShouldEndEditing: (text field) and textViewShouldEndEditing: (text view).
The primary reason for a delegate to implement these methods is to validate entered text. For example, if text should conform to a given format, the delegate validates the entered string here and returns NO if the string does not conform. The default return value is YES.
A related method for text fields is textFieldShouldReturn:. When the user taps the return key, the text field class sends a textFieldShouldReturn: message to the delegate to ask whether it should resign first responder.
-
Just after text a object resigns first responder—textFieldDidEndEditing: (text field) and textViewDidEndEditing: (text view).
浙公网安备 33010602011771号