在 UIAlertController 文本字段中传递变量
Passing a variable inside a UIAlertController textfield
我在用 UIAlertViewController 内的文本字段中写入的文本分配变量时遇到困难。即使在块执行之后,userPassword 似乎仍为 nil。这是相关代码。
__block NSString *userPassword = nil;
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.keyboardType=UIKeyboardTypeNumberPad;
textField.placeholder = @"Password";
textField.secureTextEntry = YES;
userPassword=textField.text;
}];
虽然我看不到您的所有代码,但在这种情况下,最好在 class 的顶部声明一个 public 属性、
@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.
}
}
}
我在用 UIAlertViewController 内的文本字段中写入的文本分配变量时遇到困难。即使在块执行之后,userPassword 似乎仍为 nil。这是相关代码。
__block NSString *userPassword = nil;
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.keyboardType=UIKeyboardTypeNumberPad;
textField.placeholder = @"Password";
textField.secureTextEntry = YES;
userPassword=textField.text;
}];
虽然我看不到您的所有代码,但在这种情况下,最好在 class 的顶部声明一个 public 属性、
@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.
}
}
}