可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a problem.
I have four textfields, but I don't know how to check the textfield that input String.
nameField
doesn't over 10 strings
ageField
check whether is number or not
sexField
check male or female
This is my code:
- (IBAction)addComment:(id)sender {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Commend" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Please input your name:";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Please input your age:";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Please input your sex:";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Please input your commend:";
}];
UIAlertAction *sendAction = [UIAlertAction actionWithTitle:@"send" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UITextField *nameField = alertController.textFields[0];
UITextField *ageField = alertController.textFields[1];
UITextField *sexField = alertController.textFields[2];
UITextField *commentField = alertController.textFields[3];
UIAlertController *successAlertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Success" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self.navigationController popViewControllerAnimated:YES];
}];
[successAlertController addAction:okAction];
[self presentViewController:successAlertController animated:YES completion:nil];
}];
[alertController addAction:sendAction];
[self presentViewController:alertController animated:YES completion:nil];
}
Thanks!
回答1:
Follow below steps, may it help:
Set delegate
and tag
to UIAlertController
textfields.
Now use UITextField
delegate method shouldChangeCharactersInRange
to validate text while typing OR use textFieldDidEndEditing
to check after textfield end editing . Check using it's tag to differentiate between textFields.
Like,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField.tag==TxtNameTag){
//Validate Name textField
}
//Same way for other textFields
return YES;
}
OR Use textFieldDidEndEditing
to validate while textField end editing.
回答2:
Try textField delegate method shouldChangeCharactersInRange
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// here you can get your textfield text and perform an action according to your choice while user is inputting.
return YES; //this make iOS not to perform any action
}
回答3:
Have a look on textField delegate
method shouldChangeCharactersInRange
. It will get called every time when you press any character in textfield
.
回答4:
You can implement the UITextFieldDelegate
:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Doc
回答5:
plz use the the tag propertiy of textfield
UITextField *nameField = alertController.textFields[0];
UITextField *ageField = alertController.textFields[1];
UITextField *sexField = alertController.textFields[2];
UITextField *commentField = alertController.textFields[3];
nameField.tag = 1;
ageField.tag = 2;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// identify with tag
if(textField.tag == 1)
{
}
else if (textField.tag == 2)
{
}
return YES;
}
回答6:
1.Set Delegate for UITextField.
2.Add tag.
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Commend" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Please input your name:";
textField.tag=100;
}];
3. The below Delegate will be called for every char entered.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.tag==100){
//Validate Name textField
}
else if(textField.tag==101){
//nextfield textFields
}
return YES;
}
回答7:
<your text field>.delegate = self
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
//name text field validation
if textField == nameField{
if textField.text.characters.count < 10 {
return true
}
}
//age text field validation
if textField == ageField{
if Int(textField.text) != nil {
return true
}
}
//sex text field validation
if textField == sexField{
if textField.text + replacementString == "male" {
<he is a man>
}else if textField.text + replacementString == "female"{
<she is a woman>
}
return true
}
return false
}
回答8:
[textField addTarget:self action:@selector(textDidChanged:) forControlEvents:UIControlEventEditingChanged];
like this copy also can check
回答9:
Try using JSInputField with modalViewController
JSInputField *inputField = [[JSInputField alloc] initWithFrame:CGRectMake(10, 100, 300, 50)];
[self.view addSubview:inputField];
[inputField setPlaceholder:@"Please input your name"];
[inputField setRoundedCorners:UIRectCornerAllCorners];
[inputField addValidationRule:JSCreateRuleNotNullValue]; //This will validate field for null value. It will show error if field is empty.
[inputField addValidationRule:JSCreateRuleLength(10)]; //This will validate field for string of length equal to or less than 10.
JSInputField *inputField = [[JSInputField alloc] initWithFrame:CGRectMake(10, 100, 300, 50)];
[self.view addSubview:inputField];
[inputField setPlaceholder:@"Please input your age"];
[inputField setRoundedCorners:UIRectCornerAllCorners];
[inputField addValidationRule:JSCreateRuleNotNullValue]; //This will validate field for null value. It will show error if field is empty.
[inputField addValidationRule:JSCreateRuleNumeric(0)]; //This will validate field for numeric values and restrict to enter value upto 0 decimal places.