简体   繁体   English

如何在警报视图中提示用户输入文本

[英]How to prompt user for text input in Alert view

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.我正在编写一段代码,如果我可以使用类似 UIAlertView 的弹出框并提示用户输入密码等文本,那将是最好的。 Any pointers on an elegant way of doing this?关于这样做的优雅方式的任何指示?

Things are much simpler in iOS 5, just set the alertViewStyle property to the appropriate style (UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, or UIAlertViewStyleLoginAndPasswordInput). iOS 5 中的事情要简单得多,只需将 alertViewStyle 属性设置为适当的样式(UIAlertViewStyleSecureTextInput、UIAlertViewStylePlainTextInput 或 UIAlertViewStyleLoginAndPasswordInput)。 Example:例子:

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];

在此处输入图像描述 > 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]

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];

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 ,查看UIAlertView是否有 5 个测试区,看看我们是否有可能的测试区。

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] 之后的代码将首先运行,直到您到达运行循环的末尾,然后才会显示警报。

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM