2012-11-23 39 views
2

我需要顯示一個UIAlertView,要求用戶更改一些文本,但大多數時候用戶會希望:a)保留文本原樣或b)替換整個文本。在極少數情況下,他只想通過附加或修改文本來改變文本。UIAlertView內的UITextField:如何選擇文本?

所以,爲了這個目的,我想出了下面的一段代碼:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Name for your item" 
                message:nil 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"Accept", nil]; 
alertView.alertViewStyle = UIAlertViewStylePlainTextInput; 
UITextField *textField = [alertView textFieldAtIndex:0]; 
textField.text = @"Some random text"; 
[alertView show]; 

UITextRange *textRange = [textField textRangeFromPosition:textField.beginningOfDocument 
               toPosition:textField.endOfDocument]; 
[textField setSelectedTextRange:textRange]; 

這在iPhone上正常工作,選中文本時,會顯示警告和它的作品,我想預計,但在iPad上,文本根本沒有被選中。

我做錯了什麼,這是在iOS上的錯誤,或者這種行爲記錄在某處我還沒有找到?

+0

我會記錄文本範圍變量,看看它是0還是NULL。 – CodaFi

+0

@Opsi您的代碼適用於iOS 5和iOS 6下的iPad模擬器。您的測試環境是什麼? –

+0

這很快;)事實上,似乎iPad的範圍是零而不是iPhone,所以我想這是我的時間去填補一個bug ...謝謝! – opsidao

回答

0
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"name" message:@"" delegate:self 
              cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 

alertViewt.alertViewStyle = UIAlertViewStylePlainTextInput; 
UITextField *textField = [Renamealert textFieldAtIndex:0]; 
[email protected]"XYZZZ "; 
[alertView show]; 

UITextRange *textRange = [textField textRangeFromPosition:textField.beginningOfDocument 
               toPosition:textField.endOfDocument]; 
[textField setSelectedTextRange:textRange]; 
+0

我沒有看到這個答案增加了原始問題......你能詳細說明一下嗎? – opsidao

相關問題