2016-10-11 9 views
-1

私は3つのUITextFieldでUIAlertViewを作成しました。どのように私はUIAlertViewのアクションを得ることができます。 clickedButtonAtIndexデリゲートメソッドが機能しています。UIAlertViewで3 UITextFieldを作成しましたが、動作しません。

しかし、フィールドに入力されたテキストを取得できません。何か案が ?ありがとうございます。デリゲートで

UIView *v_alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)]; 
    v_alert.tag=10; 

UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)]; 
textField1.borderStyle = UITextBorderStyleRoundedRect; 
textField1.placeholder = @"Your Previous Password"; 
textField1.keyboardAppearance = UIKeyboardAppearanceAlert; 
textField1.delegate = self; 
textField1.tag=1; 

[v_alert addSubview:textField1]; 

UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)]; 
textField2.placeholder = @"Your New Password"; 
textField2.borderStyle = UITextBorderStyleRoundedRect; 
textField2.keyboardAppearance = UIKeyboardAppearanceAlert; 
textField2.delegate = self; 
textField2.tag=2; 

[v_alert addSubview:textField2]; 


UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)]; 
textField3.placeholder = @"Email"; 
textField3.borderStyle = UITextBorderStyleRoundedRect; 
textField3.keyboardAppearance = UIKeyboardAppearanceAlert; 
textField3.delegate = self; 
textField3.tag=3; 

[v_alert addSubview:textField3]; 


alertV = [[UIAlertView alloc] initWithTitle:@"Change Password" message:@"" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil]; 
[alertV setValue:v_alert forKey:@"accessoryView"]; 
alertV.tag==100; 
alertV.delegate=self; 

[alertV show]; 

は:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
NSLog(@"didDismissWithButtonIndex"); 



     UITextField*txt1=[[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:0]; 
    UITextField*txt2= [[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:1];   
    UITextField*txt3= [[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:2]; 

    NSLog(@"%@--%@--%@",txt1.text,txt2.text,txt3.text); 
} 
そして

enter image description here

+0

alertV.delegate = self; –

+3

最初に表示されたアラートビューは、iOS 8で廃止されました。 – user3182143

+1

@ user3182143が正しいです。もしiOS8以上で作業しているのなら、 'UIAlertController'を使用する必要があります。 – Kampai

答えて

1

あなたの3のTextField

を宣言している

enter image description here

を撃っ見ます

UITextField *textField1, *textField2, *textField3; 

このコードをコードに置き換えてください。

UIView *alertview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 250)]; 
    alertview.backgroundColor = [UIColor whiteColor]; 
    alertview.tag=10; 
    textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)]; 
    textField1.borderStyle = UITextBorderStyleRoundedRect; 
    textField1.placeholder = @"Your Previous Password"; 
    textField1.secureTextEntry = YES; 
    textField1.keyboardAppearance = UIKeyboardAppearanceAlert; 
    textField1.delegate = self; 
    textField1.tag=1; 

    [alertview addSubview:textField1]; 

    textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)]; 
    textField2.placeholder = @"Your New Password"; 
    textField2.secureTextEntry = YES; 
    textField2.borderStyle = UITextBorderStyleRoundedRect; 
    textField2.keyboardAppearance = UIKeyboardAppearanceAlert; 
    textField2.delegate = self; 
    textField2.tag=2; 

    [alertview addSubview:textField2]; 


    textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)]; 
    textField3.placeholder = @"Email"; 
    textField3.borderStyle = UITextBorderStyleRoundedRect; 
    textField3.keyboardAppearance = UIKeyboardAppearanceAlert; 
    textField3.delegate = self; 
    textField3.tag=3; 

    [alertview addSubview:textField3]; 


    UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"Change Password" message:@"" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil]; 
    [alertV setValue:alertview forKey:@"accessoryView"]; 
    alertV.tag==100; 
    alertV.delegate=self; 

    [alertV show]; 

今すぐあなたのUIAlertViewのボタンメソッド

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) { 
     NSLog(@"Button No click"); 
    } 
    if (buttonIndex == 1) { 
     NSLog(@"Button Yes Cancel"); 
    } 
    NSLog(@"%@--%@--%@",textField1.text,textField2.text,textField3.text); 
} 

出力: [email protected]

+0

私は3つのフィールドを使用しました! –

+0

[alert textFieldAtIndex:3] .placeholder = @ "Email ID"に1つ以上のフィールドを追加します。パスワードを確認した後に –

+0

@ JamshedAlamはそれをチェックして、私はちょうど私の答えを更新します。 –

1

最初に設定テキストフィールドのデリゲート

<UITextFieldDelegate> 

Iは、以下のコードを試み

NSString *strAddress = @"14 UPPER CIRCULAR ROAD, Singapore 058412"; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:strAddress delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)]; 

    UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)]; 
    textField1.borderStyle = UITextBorderStyleRoundedRect; 
    textField1.placeholder = @"Username"; 
    textField1.keyboardAppearance = UIKeyboardAppearanceAlert; 
    textField1.delegate = self; 
    [v addSubview:textField1]; 

    UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)]; 
    textField2.placeholder = @"Password"; 
    textField2.borderStyle = UITextBorderStyleRoundedRect; 
    textField2.keyboardAppearance = UIKeyboardAppearanceAlert; 
    textField2.delegate = self; 
    [v addSubview:textField2]; 


    UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)]; 
    textField3.placeholder = @"Address"; 
    textField3.borderStyle = UITextBorderStyleRoundedRect; 
    textField3.keyboardAppearance = UIKeyboardAppearanceAlert; 
    textField3.delegate = self; 
    [v addSubview:textField3]; 

    [alert setValue:v forKey:@"accessoryView"]; 



    [alert show]; 

のTextFieldのデリゲートメソッド

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    NSLog(@"The delegate method is called"); 
    return YES; 
} 

また、私の画面が出力結果が

The delegate method is called 
+0

代議員が設定しました。私はテキストを入力していません。 –

+0

質問が更新されました。それを見てください。 –

0

私は受け入れトリックです。しかし、標準的な方法は最新のiOS向けです:

UIAlertController * alert= [UIAlertController 
           alertControllerWithTitle:@"Change Password" 
           message:@"" 
           preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction* ok = [UIAlertAction 
        actionWithTitle:@"OK" 
        style:UIAlertActionStyleDefault 
        handler:^(UIAlertAction * action) 
        { 

          UITextField *alertText1 = [alert.textFields objectAtIndex:0]; 
         UITextField *alertText2 = [alert.textFields objectAtIndex:1]; 
         UITextField *alertText3 = [alert.textFields objectAtIndex:2]; 


         if(alertText1.text.length>0&&alertText1.text.length>0) 
         { 
          NSLog(@"Resolving UIAlert Action for tapping OK Button:%@-%@-%@",alertText1.text,alertText2.text,alertText3.text); 

          oldpPass=alertText1.text; 

          NewPass=alertText2.text; 

          [self update_password]; 

         } 

         [alert dismissViewControllerAnimated:YES completion:nil]; 

        }]; 
UIAlertAction* cancel = [UIAlertAction 
         actionWithTitle:@"Cancel" 
         style:UIAlertActionStyleDefault 
         handler:^(UIAlertAction * action) 
         { 
          NSLog(@"Resolving UIAlertActionController for tapping cancel button"); 
          [alert dismissViewControllerAnimated:YES completion:nil]; 

         }]; 

[alert addAction:ok]; 
[alert addAction:cancel]; 



[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) { 
    textField.placeholder = @"Enter old password"; 
    textField.accessibilityIdentifier = @"paswordTextField"; 
    textField.accessibilityLabel = @"passwordLabel"; 
}]; 

[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) { 
    textField.placeholder = @"Enter New password"; 
    textField.accessibilityIdentifier = @"paswordTextField"; 
    textField.accessibilityLabel = @"passwordLabel"; 
}]; 

[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) { 
    textField.accessibilityIdentifier = @"usernameTextField"; 
    textField.placeholder = @"Enter email"; 
    textField.text=self.emailTF.text;// 
    textField.accessibilityLabel = @"usernameLabel"; 
}]; 

[self presentViewController:alert animated:YES completion:nil]; 
関連する問題