Ios 在UIAlertController文本字段内传递变量
我很难用UIAlertViewController内的文本字段中写入的文本指定变量。即使在块执行之后,userPassword似乎也显示为nil。这是相关代码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
__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.
}
}
}