Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UIAlertController文本字段内传递变量_Ios_Objective C_Block_Uialertcontroller - Fatal编程技术网

Ios 在UIAlertController文本字段内传递变量

Ios 在UIAlertController文本字段内传递变量,ios,objective-c,block,uialertcontroller,Ios,Objective C,Block,Uialertcontroller,我很难用UIAlertViewController内的文本字段中写入的文本指定变量。即使在块执行之后,userPassword似乎也显示为nil。这是相关代码 __block NSString *userPassword = nil; [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.keyboardType=UIKeyboardTypeNumberPad; t

我很难用UIAlertViewController内的文本字段中写入的文本指定变量。即使在块执行之后,userPassword似乎也显示为nil。这是相关代码

__block NSString *userPassword = nil;



[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.keyboardType=UIKeyboardTypeNumberPad;
    textField.placeholder = @"Password";
    textField.secureTextEntry = YES;
    userPassword=textField.text;

}];

虽然我看不到您的所有代码,但在这种情况下,最好在类的顶部声明公共属性

@property (nonatomic, strong) NSString *userPassword; 
然后分配它就像

self.userPassword = textField.text; 

addTextFieldWithConfigurationHandler:
用于将文本字段添加到
UIAlertController
中,其块在用户看到警报之前以及在用户有机会键入文本字段之前执行。将操作添加到警报时,
addAction:
,您可以在此处访问用户提供信息的文本字段

[alert addAction:[UIAlertAction actionWithTitle:title 
                                          style:style 
                                        handler:^(UIAlertAction *action) {
    // loop is for example, replace to suit your needs
    // probably with something like
    // [alert.textFields objectAtIndex:pwIndex].text ...
    for (UITextField *textField in alert.textFields) {
        if (textField.secureTextEntry) {
            self.userPassword = textField.text;

            // do something with the password.
        }
    }
}