2012-11-05 38 views
6

我想創建一個包含兩個uitextfields的警報視圖。如何在警報視圖中創建兩個文本框,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)是文本字段中的數組的範圍之外'

爲什麼我得到這個錯誤,我怎麼能在一個alert視圖中創建兩個uitextfields?

=========工作液=========== 感謝以下作品的答案時,你只需要兩個簡單的文本框

//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

嘗試翻轉文本框分配。首先創建一個文本框,然後執行[message textfieldatindex:0] = newTextField – Hackmodford

回答

23

在您分配「消息」警報視圖。將此代碼添加到您的代碼中:

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

這將使您的警報視圖裏面的兩個文本字段。

+0

但它似乎OP想要兩個純文本字段,而不是一個普通和一個安全。 – rmaddy

+0

@rmaddy我編輯了我的評論。請檢查。 –

+0

非常好。這是一個這樣簡單的解決方案。爲什麼我沒有想到這個? :) – rmaddy

0
+0

該解決方案複製'UIAlertView' API已經提供的內容。另外,OP需要兩個純文本字段,而不是一個普通文本字段和一個安全字段。 – rmaddy

+0

我以前試過,我發現它不是很好的解決方案,因爲它很難繪製矩形和按鈕由於某種原因不可見 –

+0

錯誤:404找不到。替換鏈接 –

3

由於UIAlertView不包含任何文本字段,因此會出現該錯誤。由於當您嘗試致電[alertView textFieldAtIndex:0]時,警報視圖的文本字段集合是空的,因此最終會出現NSInvalidArgumentException併發生崩潰。

+0

OP不創建任何文本字段。再次閱讀代碼。 – rmaddy

+0

良好的通話。這就是我匆忙回答的原因。 –

4

您收到的錯誤發生是因爲UIAlertView'消息'中沒有文本字段。實例方法「textFieldAtIndex」用於訪問使用特定樣式創建的UIAlertView中的文本字段,如UIAlertViewStylePlainTextInput,UIAlertViewStyleSecureTextInputUIAlertViewStyleLoginAndPasswordInput。這些樣式在屬性「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中創建兩個文本字段。這裏:

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]; 
相關問題