2016-08-31 26 views
1

問題があります。
私は4つのテキストフィールドを持っていますが、入力フィールドのテキストフィールドをチェックする方法はわかりません。

ユーザー入力時にtextFiled入力文字列をチェックする方法

  • nameFieldは10弦の上に
  • ageField小切手か数か
  • sexFieldチェック男性または女性

ですこれは私のコードではありません。

- (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]; 

} 

ありがとう!

+0

JSInputFieldを使用してみてくださいを確認することができますか?または、送信ボタンを押した後に? –

+0

男性または女性を確認するには –

+3

年齢と性別のフィールドでピッカーを使用しない理由 – sanjeet

答えて

1

のタグpropertiyを使用します。

  1. delegatetagUIAlertControllerのテキストフィールドを設定します。

  2. は今入力中のテキストを検証したり、テキストフィールドの端の編集後にチェックするためにtextFieldDidEndEditingを使用するUITextFieldデリゲートメソッドshouldChangeCharactersInRangeを使用します。 textFieldsを区別するためにそのタグを使ってチェックしてください。

と同様に、

- (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のTextFieldエンド編集中に検証するtextFieldDidEndEditingを使用してください。

+0

ありがとう!私を助けてください。 –

+0

素晴らしい:) @imbeginner_sorry –

0

てみたTextFieldデリゲートメソッドが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 
} 
0

textField delegate方法shouldChangeCharactersInRangeの表情を持っています。 textfieldで任意の文字を押すたびに呼び出されます。

0

あなたが実装することができUITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 

Doc

0

plzはそれが役立つことがあり、下記の手順テキストフィールド

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; 
    } 
0

1.Set UITextFieldの委任。

2.Addタグ。

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Commend" message:nil preferredStyle:UIAlertControllerStyleAlert]; 

     [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
      textField.placeholder = @"Please input your name:"; 
      textField.tag=100; 
     }]; 

3.以下のDelegateは、入力されたすべての文字に対して呼び出されます。

-(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; 
} 
0
<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 
} 
0

[テキストフィールドaddTarget:自己アクション:@selector(textDidChanged :) forControlEvents:UIControlEventEditingChanged]。このコピーのような

0

あなたは正しいデータが入力されたかされているかどうかを入力しながら確認したい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. 
関連する問題