在iPhone上获取文本输入弹出对话框的简单方法是什么 [英] What's a simple way to get a text input popup dialog box on an iPhone

查看:111
本文介绍了在iPhone上获取文本输入弹出对话框的简单方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取用户名。一个简单的文本输入对话框。有什么简单的方法吗?

I want to get the user name. A simple text input dialog box. Any simple way to do this?

推荐答案

在iOS 5中有一种新的 easy 方式这个。我不确定实现是否完全完整,因为它不是一个优雅的,比如说, UITableViewCell ,但它应该明确地做,因为它现在是标准支持在iOS API中。您不需要私有API。

In iOS 5 there is a new and easy way to this. I'm not sure if the implementation is fully complete yet as it's not a gracious as, say, a UITableViewCell, but it should definitly do the trick as it is now standard supported in the iOS API. You will not need a private API for this.

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is an example alert!" delegate:self cancelButtonTitle:@"Hide" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
[alert release];

这会呈现这样的alertView(截图取自XCode 4.2中的iPhone 5.0模拟器):

This renders an alertView like this (screenshot taken from the iPhone 5.0 simulator in XCode 4.2):

当按下任何按钮时,将调用常规委托方法,你可以像这样提取textInput:

When pressing any buttons, the regular delegate methods will be called and you can extract the textInput there like so:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    NSLog(@"Entered: %@",[[alertView textFieldAtIndex:0] text]);
}

这里我只是NSLog输入的结果。在生产代码中,您应该将指向alertView的指针保存为全局变量,或者使用alertView标记来检查委托函数是否由相应的 UIAlertView 调用,但是对于此例如,这应该没问题。

Here I just NSLog the results that were entered. In production code, you should probably keep a pointer to your alertView as a global variable or use the alertView tag to check if the delegate function was called by the appropriate UIAlertView but for this example this should be okay.

你应该看看 UIAlertView API ,你会看到还有更多样式被定义。

You should check out the UIAlertView API and you'll see there are some more styles defined.

希望这有帮助!

- 编辑 -

我是使用alertView一点点玩,我想它不需要声明完全可以根据需要编辑textField:你可以创建对 UITextField 的引用并将其编辑为正常(以编程方式)。
这样做我构建了一个你在原始问题中指定的alertView。迟到总比没有好,对吧: - )?

I was playing around with the alertView a little and I suppose it needs no announcement that it's perfectly possible to edit the textField as desired: you can create a reference to the UITextField and edit it as normal (programmatically). Doing this I constructed an alertView as you specified in your original question. Better late than never, right :-)?

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hello!" message:@"Please enter your name:" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumberPad;
alertTextField.placeholder = @"Enter your name";
[alert show];
[alert release];

这会产生此警报:

< img src =https://i.stack.imgur.com/of77H.pngalt =使用UIAlertViewPlainTextInput alertStyle询问用户名的UIAlertView>

您可以使用与我之前提到的相同的委托方法来处理输入结果。我不确定你是否可以阻止 UIAlertView 解散(没有 shouldDismiss 委托函数AFAIK)所以我想如果用户输入无效,你必须提出一个新警报(或者只需重新显示这个),直到输入正确的输入。

You can use the same delegate method as I poster earlier to process the result from the input. I'm not sure if you can prevent the UIAlertView from dismissing though (there is no shouldDismiss delegate function AFAIK) so I suppose if the user input is invalid, you have to put up a new alert (or just reshow this one) until correct input was entered.

玩得开心!

这篇关于在iPhone上获取文本输入弹出对话框的简单方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆