2013-09-27 90 views
0

帶UITextField的UIAlertView無法在iOS 7中工作。如何解決?iOS 7中的UITextField問題

下面是截圖:

​​ 這裏是我的代碼:

我怎樣才能在iOS7得到UITextFeield在UIAlertView中?

-(void)takeUserName 
{ 
     mChangePlayerAlert = [[UIAlertView alloc] init]; 
     mChangePlayerAlert.title = title; 
     mChangePlayerAlert.message = @"\n"; 
     mChangePlayerAlert.delegate = self; 
     [mChangePlayerAlert addButtonWithTitle:@"Save"]; 
     [mChangePlayerAlert addButtonWithTitle:@"Cancel"]; 

     CGRect frame; 
     frame = CGRectMake(20, 45, 245, 27);//(200, 500, 400, 120); 

     mTextFeild = [[UITextField alloc] initWithFrame:frame]; 
     mTextFeild.textColor = [UIColor blueColor]; 
     mTextFeild.borderStyle = UITextBorderStyleRoundedRect; 

     mTextFeild.keyboardType = inType;//; 
     mTextFeild.returnKeyType = UIReturnKeyDone; 
     mTextFeild.autocorrectionType = UITextAutocorrectionTypeNo; 
     mTextFeild.autocapitalizationType = UITextAutocapitalizationTypeNone; 
     mTextFeild.delegate = self; 

     [mChangePlayerAlert addSubview:mTextFeild]; 

     mTextFeild.delegate = self; 
     [mTextFeild becomeFirstResponder]; 

     [mChangePlayerAlert show]; 
} 

回答

2

您不應該使用文本視圖。

mChangePlayerAlert.alertViewStyle = UIAlertViewStylePlainTextInput;

要在以後得到的值,使用textFieldAtIndex:在警報視圖代表:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    if(buttonIndex != alertView.cancelButtonIndex) 
    { 
     NSString* text = [alertView textFieldAtIndex:0].text; 

     ... 
    } 
} 
+1

請參閱編輯我的答案。 –

+0

哇..其工作感謝...一個小問題,上次我使用shouldChangeCharactersInRange textFeild委託來限制文本的數量。現在,代表不打電話...我怎樣才能限制文本的數量在這個文本框? – iPhoneProcessor

+1

你可以。在顯示警報視圖之前,將'[alertView textFieldAtIndex:0]'的委託設置爲您自己的。現在,像以前一樣,當用戶添加字符時,您將會被調用。 –

1

如果以上回答不適合你試試這個

[txtvwMessage setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; 

一般iOS6的到iOS7轉換失去了設計控制。這就是爲什麼自動調整大小的概念是有幫助你在轉換

0

試試這個。

- (IBAction)showAlert:(id)sender 
{ 
    UIAlertView *alertView = [[UIAlertView alloc] init]; 

    switch (((UIButton *)sender).tag) 
    { 
    // Default Alert View 
    case 1001: 

     alertView.title = @"Default Alert View"; 
     alertView.message = @"UIAlertViewStyleDefault"; 
     [alertView addButtonWithTitle:@"OK"]; 

     break; 

    // Secure Alert View 
    case 1002: 

     alertView.title = @"Secure Alert View"; 
     alertView.message = @"UIAlertViewStyleSecureTextInput"; 
     alertView.alertViewStyle = UIAlertViewStyleSecureTextInput; 
     [alertView addButtonWithTitle:@"OK"]; 
     [alertView addButtonWithTitle:@"Cancel"]; 

     break; 

    // Plain Alert View 
    case 1003: 

     alertView.title = @"Plain Alert View"; 
     alertView.message = @"UIAlertViewStylePlainTextInput"; 
     alertView.alertViewStyle = UIAlertViewStylePlainTextInput; 
     [alertView addButtonWithTitle:@"OK"]; 
     [alertView addButtonWithTitle:@"Cancel"]; 

     break; 

    // Login ALert View 
    case 1004: 

     alertView.title = @"Login Alert View"; 
     alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; 
     [alertView addButtonWithTitle:@"OK"]; 
     [alertView addButtonWithTitle:@"Cancel"]; 

     break; 
    } 

    [alertView show]; 
}