2011-07-18 80 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