2012-11-05 14 views
6

アラートビュー内に2つのuitextfieldを含むalertviewを作成したいとします。アラートビューで2つのテキストフィールドを作成する方法、IOS

method: 
//show alertview for file input 
- (IBAction)showAddFiles:(id)sender { 
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Enter File Details" 
                 message:nil 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"Add", nil]; 



    UITextField *textFieldDescription = [message textFieldAtIndex:0]; 
    textFieldDescription.placeholder = @"File Description : Ex. Acat Briefing"; 
    UITextField *textFieldFileName = [message textFieldAtIndex:1]; 
    textFieldFileName.placeholder = @"Exact File Name : Ex. acat.pdf"; 


    [message show]; 
} 


//make sure file description is long enoguh 
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView 
{ 
    NSString *inputText = [[alertView textFieldAtIndex:0] text]; 

    if([inputText length] <= 15 && [inputText length] >= 4) 
    { 
     return YES; 
    } 
    else 
    { 
     return NO; 
    } 
} 

//handle add button 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 
    if([title isEqualToString:@"Add"]) 
    { 
     UITextField *fileDescription = [alertView textFieldAtIndex:0]; 
     UITextField *fileName = [alertView textFieldAtIndex:1]; 
     NSLog(@"Desc: %@\nName: %@", fileDescription.text, fileName.text); 
    } 
} 

エラー:

*キャッチされない例外によるアプリ 'NSInvalidArgumentException' を終端する理由:

'textFieldIndex(0)テキストフィールドの配列の範囲の外側にあります'

なぜこのエラーが発生するのですか?アラートビューで2つのuitextfieldを作成するにはどうすればよいですか?

=========ワーキングソリューション===========作品以下の答えのための おかげで、あなただけここでは、2つのプレーンテキストフィールド

//show alertview for file input 
- (IBAction)showAddFiles:(id)sender { 
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Enter File Details" 
                 message:nil 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"Add", nil]; 



    [message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; 
    UITextField *fileDescription = [message textFieldAtIndex:0]; 
    [email protected]"Ex. acat.pdf"; 
    [[message textFieldAtIndex:1] setSecureTextEntry:NO]; 
    UITextField *fileName= [message textFieldAtIndex:1]; 
    [email protected]"Ex. Acat Briefing"; 

    [message show]; 
} 
+1

は、テキストフィールドの割り当てを反転してください。最初にテキストフィールドを作成してからdo [message textfieldatindex:0] = newTextField – Hackmodford

答えて

23

"メッセージ"アラートビューを割り当てた後。これをコードに追加してください:

[message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; 
[[message textFieldAtIndex:1] setSecureTextEntry:NO]; 

これにより、アラートビュー内に2つのテキストフィールドが表示されます。

+0

しかし、OPは2つのプレーンテキストフィールドを必要とし、1つのプレーンテキストと1つのセキュアではありません。 – rmaddy

+0

@rmaddy私のコメントを編集しました。チェックしてください。 –

+0

優れています。それはとても簡単な解決策です。なぜ私はそれを考えなかったのですか? :) – rmaddy

0
+0

このソリューションは、 'UIAlertView' APIによって既に提供されているものを複製します。さらに、OPは2つのプレーンテキストフィールドを必要とします。 – rmaddy

+0

私はこれを前に試しましたが、それは難しいので、それは良い解決策ではないことを発見しましたrectとボタンは何らかの理由で表示されません –

+0

エラー:404が見つかりません。 replace –

3

UIAlertViewは、任意のテキストフィールドが含まれていないので、あなたはそのエラーを取得しています。 [alertView textFieldAtIndex:0]を呼び出そうとすると、アラートビューのテキストフィールドコレクションが空であるため、NSInvalidArgumentExceptionとクラッシュすることになります。あなたのUIAlertView、「メッセージ」にはテキストフィールドが存在しないため

+0

OPはテキストフィールドを作成しません。コードをもう一度読んでください。 – rmaddy

+0

よろしくお願いします。それは私が急いで答えるために得るものです。 –

4

表示されたエラーが発生します。インスタンスメソッド「textFieldAtIndexは」UIAlertViewStylePlainTextInputUIAlertViewStyleSecureTextInput、またはUIAlertViewStyleLoginAndPasswordInputのように、特定のスタイルを使用して作成されたUIAlertViewでテキストフィールドにアクセスするために存在しています。これらのスタイルは、プロパティ "alertViewStyle"で設定されます。たとえば:

[message setAlertViewStyle:UIAlertViewStylePlainTextInput]; 

このプロパティを設定した後、「textFieldAtIndex」を使用することができますが、これらのスタイルのどれもが、あなたのニーズに合わせていないかのように、残念ながらそれが見えます。

私が以前に行ったことは、既定のスタイルのUIAlertViewを(あなたがすでに行ったように)作成し、UITextFieldsをUIAlertViewにサブビューとして追加することです。例については

//Create the alert then add any labels and text fields as subviews. 
//You can pad out an alertView by adding newline characters in the message. This will 
// give the alertView more space to draw the text fields. 
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Two Text Field Alert" 
                 message:@"\n\n\n\n\n" 
                 delegate:self 
               cancelButtonTitle:@"CanceL" 
               otherButtonTitles:@"OK", nil]; 

UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)]; 
textField1.borderStyle = UITextBorderStyleRoundedRect; 
textField1.keyboardAppearance = UIKeyboardAppearanceAlert; 
textField1.delegate = self; 
[message addSubview:textField1]; 

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

[message release]; 
[textField2 release]; 
[textField1 release]; 

これは、ログインにalertViewスタイルとは対照的に、この方法を実行するために多くの、より詳細なと厄介だが、あなたがするサブビューの任意の数を追加するには合うようにあなたはこれを適応することができますアラートビュー。

は例を簡単にするために編集されました。

1

我々はalertviewで唯一の2つのテキストフィールドを作成するために許可されています。ここで:

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

alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; 
UITextField * alertTextField1 = [alert textFieldAtIndex:0]; 
alertTextField1.keyboardType = UIKeyboardTypeDefault; 
alertTextField1.placeholder = @"Type Current password"; 
[[alert textFieldAtIndex:0] setSecureTextEntry:YES]; 

UITextField * alertTextField2 = [alert textFieldAtIndex:1]; 
alertTextField2.keyboardType = UIKeyboardTypeDefault; 
alertTextField2.placeholder = @"Type New Password"; 

[alert show]; 
関連する問題