2011-07-18 83 views
0

當用戶按下我的「導入」按鈕時,他們必須能夠輸入一個URL,然後可以「確定」或「取消」。UIAlertView中的iOS/UITextField

我該怎麼做?

很明顯,我可以創建一個包含文本字段和2個按鈕的新視圖。但是這看起來像是過度編碼。

一個解決方案,我發現涉及 '黑客' 爲UITextField到UIAlertView中:http://iphone-dev-tips.alterplay.com/2009/12/username-and-password-uitextfields-in.html

(編輯:好 - http://www.iphonedevsdk.com/forum/iphone-sdk-development/1704-uitextfield-inside-uialertview.html#post10643

這看起來真難看。這顯然是一個黑客。

任何人都可以提供更好的解決方案(甚至更好地實現相同的解決方案路徑)嗎?

+0

呃..你爲什麼不使用'textfield.text'or我我不理解你的權利? –

+0

對不起,我的措辭不好。我編輯了這個問題。 –

+0

此處還討論了http://stackoverflow.com/questions/376104/uitextfield-in-uialertview-on-iphone-how-to-make-it-responsive – neoneye

回答

2

確定後一噸的挖,這裏是結果。首先,將'UITextField UIAlertView'放入SO的搜索中會返回數十個匹配結果。

事實證明有這樣做的方法,Need new way to add a UITextField to a UIAlertView但它是私人API:

幾乎所有其他的解決方案涉及黑客一個UIAlertView中,這是醜陋: http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html
http://iphone-dev-tips.alterplay.com/2009/12/username-and-password-uitextfields-in.html
https://github.com/josecastillo/EGOTextFieldAlertView/
How to move the buttons in a UIAlertView to make room for an inserted UITextField?
^ MrGando的答案是整齊
http://iphonedevelopment.blogspot.com/2009/02/alert-view-with-prompt.html
Getting text from UIAlertView

唯一正確的解決方案我發現(即從一個UIView從頭編碼)在這裏:https://github.com/TomSwift/TSAlertView

這是所有需要:

- (IBAction) importTap 
{ 
    TSAlertView* av = [[[TSAlertView alloc] init] autorelease]; 
    av.title = @"Enter URL"; 
    av.message = @""; 

    [av addButtonWithTitle: @"Ok"]; 
    [av addButtonWithTitle: @"Cancel"]; 


    av.style = TSAlertViewStyleInput; 
    av.buttonLayout = TSAlertViewButtonLayoutNormal; 
    av.delegate = self; 

    av.inputTextField.text = @"http://"; 

    [av show]; 
} 

// after animation 
- (void) alertView: (TSAlertView *) alertView 
didDismissWithButtonIndex: (NSInteger) buttonIndex 
{ 
    // cancel 
    if(buttonIndex == 1) 
     return; 

    LOG(@"Got: %@", alertView.inputTextField.text); 
} 

或Presto!

enter image description here

+2

實際上,UIAlertView中的文本字段將在iOS中受支持5.這是唯一的警告,雖然,iOS 5. – Espresso

+1

re:實際上,UIAlertViews中的文本字段將在iOS 5中受支持。http://mobile.tutsplus.com/tutorials/iphone/ios-5-sdk-uialertview-text - 輸入和驗證/ – OscarTheGrouch