2013-12-11 45 views
0

我正在實現一個簡單的對話框,要求輸入名稱。這是我的代碼:UITextField在輸入對話框中不顯示

UIAlertView* dialog = [[UIAlertView alloc] init]; 
[dialog setDelegate:self]; 
[dialog setTitle:@"Enter Name"]; 
[dialog setMessage:@" "]; 
[dialog addButtonWithTitle:@"Cancel"]; 
[dialog addButtonWithTitle:@"OK"]; 

UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)]; 
[nameField setBackgroundColor:[UIColor whiteColor]]; 
[dialog addSubview:nameField]; 
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 20.0); 
[dialog setTransform: moveUp]; 
[dialog show]; 
[dialog release]; 
[nameField release]; 

它曾經工作正常,但在iOS 7的文本框不顯示。誰能幫忙?

回答

2

這不會在iOS7 工作了,試試這個,而不是

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"Enter a phone number",nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel",nil) otherButtonTitles:NSLocalizedString(@"OK",nil), nil]; 
       alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
       [alert setTag:1]; 
       [[alert textFieldAtIndex:0] setDelegate:self]; 
       [[alert textFieldAtIndex:0] resignFirstResponder]; 
       [[alert textFieldAtIndex:0] setPlaceholder:NSLocalizedString(@"Enter Number here", nil)]; 
       [[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad]; 
       [[alert textFieldAtIndex:0] becomeFirstResponder]; 

       [alert show]; 
       [alert release]; 

你可以改變你的鍵盤類型爲您的要求。

祝你好運:)

+0

三江源,是偉大工程 – Booms

0

嘗試像這樣使用...並且沒有必要添加文本字段作爲子視圖。

UIAlertView* dialog = [[UIAlertView alloc] init]; 
       [dialog setDelegate:self]; 
       [dialog setTitle:@"Enter Name"]; 
       [dialog setMessage:@" "]; 
       [dialog addButtonWithTitle:@"Cancel"]; 
       [dialog addButtonWithTitle:@"OK"]; 

       dialog.alertViewStyle = UIAlertViewStylePlainTextInput; 

CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 20.0); 
       [dialog setTransform: moveUp]; 
       [dialog show]; 
[dialog release]; 
[nameField release]; 
+0

三江源,完美的作品 – Booms

+0

如果它完美的作品,爲什麼你取消接受這個答案,你沒有給予好評吧.. –