Подтвердить что ты не робот

Как запросить пользователя для ввода текста в окне "Предупреждение"

Я пишу раздел кода, где было бы лучше, если бы я мог использовать всплывающее окно вроде UIAlertView и предлагать пользователю вводить текст как пароль. Любые указатели на элегантный способ сделать это?

4b9b3361

Ответ 1

Лучший способ, который я нашел для этого, - следовать этому руководству: http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html

enter image description here

Используемый для этого код (взятый непосредственно из этого большого учебника):

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

Ответ 2

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

Ответ 3

enter image description here > Просто Вы можете применить, как это

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]

Ответ 4

Если мое приложение не должно быть выпущено еще пару месяцев или два, я должен войти в http://developer.apple.com, посмотреть на iOS 5 beta, и посмотрите, может ли UIAlertView что-то хранить для нас.

Ответ 5

Я думаю, было бы полезно знать, что UIAlertView не является модальным, поэтому предупреждение не будет блокироваться.

Я столкнулся с этой проблемой, когда я хотел пригласить пользователя для ввода, затем продолжить, а затем использовать этот ввод в коде после. Но вместо этого код после [show alert] будет запускаться первым, пока вы не достигнете конца цикла цикла, тогда появится предупреждение.

Ответ 6

Оптимизированный код:

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