【问题标题】:iOS / UITextField in UIAlertViewUIAlertView 中的 iOS / UITextField
【发布时间】:2011-07-18 16:17:14
【问题描述】:

当用户按下我的“导入”按钮时,他们必须能够输入 URL,然后他们可以“确定”或“取消”。

我该怎么做?

显然我可以创建一个包含一个文本字段和 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

这看起来真的很难看。这显然是一个 hack。

谁能提供更好的解决方案(甚至是相同解决方案路径的更好实现)?

【问题讨论】:

  • Erm.. 你为什么不用textfield.text或者我不理解你的意思?
  • 对不起,我的措辞不好。我已经编辑了问题。

标签: ios uitextfield uialertview


【解决方案1】:

好的,经过大量挖掘,结果如下。

首先,将 '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?
^ 甘多先生的回答很简洁
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!

【讨论】:

  • 实际上,UIAlertViews 中的文本字段将在 iOS 5 中得到支持。不过,这是唯一的警告,iOS 5。
  • re:其实iOS 5会支持UIAlertViews中的文本字段mobile.tutsplus.com/tutorials/iphone/…
【解决方案2】:
【解决方案3】:

我在我的博客中创建了一篇关于“如何将 UITextField 从 XIB 添加到 UIAlertView”的帖子。使用 XIB 添加比纯编码更容易。您可以在自定义的小视图中添加任何内容,然后使用 1 行代码将小视图添加到 AlertView 中。请参考以下链接。

http://creiapp.blogspot.com/2011/08/how-to-add-uitextfield-to-uialertview.html

【讨论】:

    猜你喜欢
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多