我正在编写一段代码,如果我可以使用类似UIAlertView的弹出框并提示用户输入密码等文本,那将是最好的。 有关优雅方式的任何指示吗?
答案 0 :(得分:117)
在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];
答案 1 :(得分:28)
>简单你可以像这样申请
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]
答案 2 :(得分:16)
我发现这样做的最好方法是遵循本教程:http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html
用于实现此目的的代码(直接来自那个伟大的教程):
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];
答案 3 :(得分:6)
如果我的应用程序尚未发布一两个月,那么我将登录http://developer.apple.com,查看iOS 5测试版区域,看看UIAlertView
是否可能存在某些内容对我们来说。
答案 4 :(得分:3)
我认为知道UIAlertView不是模态的会有所帮助,因此警报不会阻止。
我遇到了这个问题,我想提示用户输入然后继续,然后在代码中使用该输入。但是,[alert show]之后的代码将首先运行,直到您到达运行循环结束,然后将显示警报。
答案 5 :(得分:1)
优化代码:
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];