从iOS应用程序中的UIAlertView文本框中检索值

opt*_*six 0 objective-c ios

我有一个iOS应用程序,我最近更新,以处理UIAlertView/SubView问题,导致文本框呈现为清晰或白色(或根本不渲染,不确定哪个).无论如何,这是一个相对简单的问题,因为我是Obj-C的新手,但我如何从应用程序中的另一个调用中获取新文本框的值?

这是我的UIAlertView:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Password" 
                         message:@"Enter your Password\n\n\n"  
                         delegate:self 
                         cancelButtonTitle:@"Cancel" 
                         otherButtonTitles:@"Login", nil];
alert.frame = CGRectMake( 0, 30, 300, 260);
Run Code Online (Sandbox Code Playgroud)

它曾经存储为UITextField,然后作为子视图添加到UIAlertView:

    psswdField = [[UITextField alloc] initWithFrame:CGRectMake(32.0, 65.0, 220.0, 25.0)];
    psswdField.placeholder = @"Password";
    psswdField.secureTextEntry = YES;
    psswdField.delegate = self;
    psswdField.tag = 1;
    [psswdField becomeFirstResponder];
    [alert addSubview:psswdField];
    [alert show];
    [alert release];
Run Code Online (Sandbox Code Playgroud)

这一切都已经注释掉了,而我把它重写为:

 alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
Run Code Online (Sandbox Code Playgroud)

这是我用来检索值的方式:

[psswdField resignFirstResponder];
[psswdField removeFromSuperview];

activBkgrndView.hidden = NO;
[activInd startAnimating];
[psswdField resignFirstResponder];
[self performSelectorInBackground:@selector(loadData:) withObject:psswdField.text];
Run Code Online (Sandbox Code Playgroud)

现在我对如何从该文本框中获取值发送到loadData感到困惑.

Gav*_*vin 5

您不希望将自己的文本字段添加到警报视图中.您不应该直接将子视图添加到UIAlertView.alertViewStyleUIAlertView上有一个要设置的属性UIAlertViewStyleSecureTextInput,它将为您添加一个文本字段.所以你要用这样的一行设置它:

alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
Run Code Online (Sandbox Code Playgroud)

然后,您将使用委托方法检索此文本字段中的值- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex,您必须将该方法添加到您设置为UIAlertView委托的类中.以下是该委托方法的示例实现:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    // Make sure the button they clicked wasn't Cancel
    if (buttonIndex == alertView.firstOtherButtonIndex) {
        UITextField *textField = [alertView textFieldAtIndex:0];

        NSLog(@"%@", textField.text);
    }
}
Run Code Online (Sandbox Code Playgroud)