2014-04-21 44 views
2

嗨我想查看UIAlertView中的兩個文本字段,但它顯示錯誤。我已經使用下面的代碼來查看文本字段。在UIAlertView試圖查看兩個文本字段。它顯示文本字段數組的錯誤界限iOS7

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hello!" 
                message:@"Please enter your details:" 
                delegate:self 
              cancelButtonTitle:@"Continue" 
              otherButtonTitles:nil]; 
    alert.alertViewStyle = UIAlertViewStylePlainTextInput; 

    UITextField * name = [alert textFieldAtIndex:0]; 
    name.keyboardType = UIKeyboardTypeAlphabet; 
    name.placeholder = @"Enter your name"; 

    UITextField * phone = [alert textFieldAtIndex:1]; 
    phone.keyboardType = UIKeyboardTypeNumberPad; 
    phone.placeholder = @"Enter your phone"; 

    [alert show]; 
    [alert release]; 
} 

我已經使用上面的代碼來查看兩個文本文件,但我越來越喜歡。 'textFieldIndex(1)超出了文本字段數組邊界的範圍'

請告訴我在上面的代碼中我做錯了什麼,請告訴我顯示解決此問題。

+2

檢查這個http://stackoverflow.com/questions/18886048/ios-multiple-text-fields-on-a-uialertview-in -IOS 7 –

回答

2

UIAlertView.h文件

/*索引檢索在文本字段 - 提高NSRangeException時 textFieldIndex是出界外。索引0處的字段將是 的第一個文本字段(單個字段或登錄字段), 索引1處的字段將是密碼字段。 */

- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex NS_AVAILABLE_IOS(5_0); 

這樣做:

alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; 

UITextField * name = [alert textFieldAtIndex:0]; 
name.keyboardType = UIKeyboardTypeAlphabet; 
name.placeholder = @"Enter your name"; 

UITextField * phone = [alert textFieldAtIndex:1]; 
phone.secureTextEntry = NO; 
phone.keyboardType = UIKeyboardTypeNumberPad; 
phone.placeholder = @"Enter your phone";