ios 如何在警报视图中提示用户输入文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7015842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prompt user for text input in Alert view
提问by Nirma
I am writing a section of code where it would be best if I could use a pop up box something like UIAlertView and prompt the user to enter text like a password. Any pointers on an elegant way of doing this?
我正在编写一段代码,如果我可以使用像 UIAlertView 这样的弹出框并提示用户输入诸如密码之类的文本,那将是最好的。关于这样做的优雅方式的任何指示?
采纳答案by PengOne
The best way that I've found to do this is to follow this tutorial: http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html
我发现这样做的最佳方法是遵循本教程:http: //junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html
The code used to achieve this is (taken directly from that great tutorial):
用于实现此目的的代码是(直接取自那个很棒的教程):
UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:@"Server Password" message:@"\n\n\n"
delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel",nil) otherButtonTitles:NSLocalizedString(@"OK",nil), nil];
UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,40,260,25)];
passwordLabel.font = [UIFont systemFontOfSize:16];
passwordLabel.textColor = [UIColor whiteColor];
passwordLabel.backgroundColor = [UIColor clearColor];
passwordLabel.shadowColor = [UIColor blackColor];
passwordLabel.shadowOffset = CGSizeMake(0,-1);
passwordLabel.textAlignment = UITextAlignmentCenter;
passwordLabel.text = @"Account Name";
[passwordAlert addSubview:passwordLabel];
UIImageView *passwordImage = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"passwordfield" ofType:@"png"]]];
passwordImage.frame = CGRectMake(11,79,262,31);
[passwordAlert addSubview:passwordImage];
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
passwordField.font = [UIFont systemFontOfSize:18];
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.secureTextEntry = YES;
passwordField.keyboardAppearance = UIKeyboardAppearanceAlert;
passwordField.delegate = self;
[passwordField becomeFirstResponder];
[passwordAlert addSubview:passwordField];
[passwordAlert setTransform:CGAffineTransformMakeTranslation(0,109)];
[passwordAlert show];
[passwordAlert release];
[passwordField release];
[passwordImage release];
[passwordLabel release];
回答by xcoder
Things are much simpler in iOS 5, just set the alertViewStyle property to the appropriate style (UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, or UIAlertViewStyleLoginAndPasswordInput). Example:
在 iOS 5 中事情要简单得多,只需将 alertViewStyle 属性设置为适当的样式(UIAlertViewStyleSecureTextInput、UIAlertViewStylePlainTextInput 或 UIAlertViewStyleLoginAndPasswordInput)。例子:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Password" message:@"Enter your password:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
UITextField *passwordTextField = [alertView textFieldAtIndex:0];
[alertView show];
回答by Vineesh TP
> Simple You can apply like this
> 简单你可以这样申请
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Filename" message:@"Enter the file name:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *passwordTextField = [alertView textFieldAtIndex:0];
[alertView show]
回答by PeyloW
If my app was not to be released for yet a months or two, then I would login to http://developer.apple.com, look at the iOS 5 beta area, and see if UIAlertView
might have something in store for us.
如果我的应用在一两个月内还没有发布,那么我会登录http://developer.apple.com,查看 iOS 5 测试版区域,看看是否UIAlertView
可以为我们提供一些东西。
回答by WWWWWWWWWWWWWWWWWWWWW
I think it would be helpful to know that UIAlertView is not modal so the alert will not block.
我认为知道 UIAlertView 不是模态会很有帮助,因此警报不会阻止。
I ran into this problem where I wanted to prompt the user for input then continue and then use that input in the code after. But instead the code after the [alert show] would run first until you reached the end of the run loop then the alert would display.
我遇到了这个问题,我想提示用户输入然后继续,然后在代码中使用该输入。但是,[alert show] 之后的代码将首先运行,直到您到达运行循环的末尾,然后才会显示警报。
回答by Dmitry
Optimized code:
优化代码:
UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:@"Password"
message:@"Please enter the password:\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel",nil)
otherButtonTitles:NSLocalizedString(@"OK",nil), nil];
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
passwordField.borderStyle = UITextBorderStyleRoundedRect;
passwordField.secureTextEntry = YES;
passwordField.keyboardAppearance = UIKeyboardAppearanceAlert;
passwordField.delegate = self;
[passwordField becomeFirstResponder];
[passwordAlert addSubview:passwordField];
[passwordAlert show];
[passwordAlert release];
[passwordField release];