2011-11-28 60 views
1

我使用此代碼在我的UIAlertView中實現的UITextField:UIAlertView中使用的UITextField和長消息

UIAlertView *receivedAlert = [[UIAlertView alloc] initWithTitle:message message:[NSString stringWithFormat:@"%@\n\n", [itemNameRows objectAtIndex:currentItem]] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Submit", nil]; 

receivedField = [[UITextField alloc] initWithFrame:CGRectMake(16,70,252,25)]; 
receivedField.keyboardType = UIKeyboardTypeNumberPad; 
receivedField.textAlignment = UITextAlignmentCenter; 
receivedField.borderStyle = UITextBorderStyleRoundedRect; 
receivedField.keyboardAppearance = UIKeyboardAppearanceAlert; 

[receivedAlert addSubview:receivedField]; 
[receivedAlert show]; 
[receivedAlert release]; 

時,我有一個消息,是超過2線長,就會出現問題。警報視圖正確調整大小,但文本字段不會向下移動以容納較長的行。有任何想法嗎?

我可以在縮短消息字符串的情況下解決,如果必須的話。

回答

2

您不應該將子視圖添加到UIAlertView。蘋果的文檔明確禁止它(「這個類的視圖層次是私人的,不能被修改」),除了可能讓你被拒絕外,還可能在未來的操作系統更新中崩潰(這已經發生過一次,已經發生在iOS 3.1 )。

iOS 5添加了警報視圖樣式,以便您可以以安全的方式在警報視圖中使用文本字段。除了UIAlertViewStyleDefault之外,還有UIAlertViewStyleSecureTextInput,UIAlertViewStylePlainTextInputUIAlertViewStyleLoginAndPasswordInput

創建警報視圖並將alertViewStyle屬性設置爲適當的值。然後您可以使用-textFieldAtIndex:來獲取文本字段。安全和純文本樣式在索引0處具有單個文本字段,默認沒有文本字段,登錄名和密碼在索引0(登錄)和1(密碼)處具有兩個文本字段。

查看UIAlertView Class Reference瞭解更多信息。