iOS / UIAlertView 中的 UITextField

发布于 2024-11-25 00:43:21 字数 713 浏览 0 评论 0原文

当用户按下我的“导入”按钮时,他们必须能够输入 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

这看起来真的很难看。这显然是一个黑客行为。

任何人都可以提供更好的解决方案(甚至是同一解决方案路径的更好实现)吗?

When the user presses my 'import' button, they must be able to type in a URL, which they can then 'ok' or 'cancel'.

How do I do this?

Obviously I could create a new view containing a text field and 2 buttons. But this seems like over coding.

One solution I found involves ' hacking ' a UITextField into a UIAlertView: http://iphone-dev-tips.alterplay.com/2009/12/username-and-password-uitextfields-in.html

(EDIT: Better -- http://www.iphonedevsdk.com/forum/iphone-sdk-development/1704-uitextfield-inside-uialertview.html#post10643 )

This looks really ugly. It is clearly a hack.

Can anyone provide a better solution (or even a better implementation of the same solution path)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无需解释 2024-12-02 00:43:21

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

首先,将“UITextField UIAlertView”放入 SO 的搜索中会返回数十个命中。

事实证明有一种方法可以做到这一点, 需要将 UITextField 添加到 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/
如何移动 UIAlertView 中的按钮以为插入的 UITextField 腾出空间?
^ MrGando 的回答很简洁
http://iphonedevelopment.blogspot.com/2009/02/ Alert-view-with-prompt.html

我发现的唯一正确的解决方案(即从 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 );
}

和急!

在此处输入图像描述

OK After a ton of digging, here is the result.

Firstly, putting in 'UITextField UIAlertView' into SO's search returns dozens of hits.

It turns out there is a method for doing this, Need new way to add a UITextField to a UIAlertView but it is private API :|

Pretty much every other solution involves hacking a UIAlertView, which is ugly:
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's answer is neat
http://iphonedevelopment.blogspot.com/2009/02/alert-view-with-prompt.html
Getting text from UIAlertView

The only proper solution I found (ie coding from scratch from a UIView) is here: https://github.com/TomSwift/TSAlertView

This is all it takes:

- (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 );
}

and Presto!

enter image description here

⒈起吃苦の倖褔 2024-12-02 00:43:21

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

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

I have created a post in my blog on the topic "How to add UITextField to UIAlertView from XIB". Using XIB to add is easier than pure coding. You can add whatever in a customized small view, then add the small view into the AlertView using 1 line code. Please refer to the following link.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文