如何提示用户在Alert视图中input文字

我正在编写一段代码,如果我可以使用类似UIAlertView的popup框,并提示用户input文本(如密码),那么这是最好的。 任何指针在这样做的优雅方式?

我发现这样做的最好方法是遵循本教程: 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]; 

事情在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]; 

在这里输入图像说明 >简单你可以这样申请

 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] 

如果我的应用程序不会被释放一两个月,那么我将login到http://developer.apple.com ,查看iOS 5testing版区域,看看UIAlertView是否可以为我们存储一些东西。

我认为这将是有益的,知道UIAlertView是不是模式,所以警报不会阻止。

我遇到了这个问题,我想提示用户input,然后继续,然后在代码中使用该input。 但是,[alert show]之后的代码会先运行,直到运行循环结束,然后显示警报。

优化代码:

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