Here's the problem up front: 我有一个有文本字段的UIAlertController . 我想在用户触摸警报中的"Confirm"按钮时将该文本字段的内容保存为NSString . 但是,当执行确认操作块时,警报为零(可能已经解除并在该点解除分配),因此它的文本字段也是如此,这意味着我无法保存文本字段的文本 .
我正在使用一系列UIAlertControllers来允许用户为我的应用程序创建密码,这样,只要应用程序到达前台,就会在使用应用程序之前提示用户输入代码 .
我创建了一个UIAlertController类,它有几个方便的方法,可以返回我需要使用的预配置警报 . 这是其中之一:
+ (UIAlertController*)passcodeCreationAlertWithConfirmBehavior:(void(^)())confirmBlock andCancelBehavior:(void(^)())cancelBlock {
UIAlertController *passcodeCreationAlert = [UIAlertController alertControllerWithTitle:@"Enter a passcode"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[passcodeCreationAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.keyboardType = UIKeyboardTypeNumberPad;
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
if (cancelBlock) {
cancelBlock();
}
}];
UIAlertAction* confirmAction = [UIAlertAction actionWithTitle:@"Confirm"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (confirmBlock) {
confirmBlock();
}
}];
[passcodeCreationAlert addAction:cancelAction];
[passcodeCreationAlert addAction:confirmAction];
passcodeCreationAlert.preferredAction = confirmAction;
confirmAction.enabled = NO;
return passcodeCreationAlert;
}
此方法返回一个UIAlertController,允许用户在文本字段中输入所需的密码 . 当我在视图控制器中调用此方法时,我将块作为参数传递,用作UIAlertAction处理程序:
- (void)presentCreatePasscodeAlert {
UIAlertController *alert = [UIAlertController passcodeCreationAlertWithConfirmBehavior:^{
firstPasscode = alert.textFields[0].text;
[self presentConfirmPasscodeAlert];
} andCancelBehavior:^{
[self presentEnablePasscodeAlert];
}];
alert.textFields[0].delegate = self;
[self presentViewController:alert animated:YES completion:nil];
}
To reiterate the problem now that there is more context: 在行中输入操作块时:
firstPasscode = alert.textFields[0].text;
警报为零,文本字段也是如此,这意味着我无法保存文本字段的文本 .
但是,在一个单独的项目中,我尝试在不使用类别和自定义便捷方法的情况下获得相同的功能,并且可以按照需要运行:
- (void) createPassword {
UIAlertController *createPasswordAlert = [UIAlertController alertControllerWithTitle:@"Enter a password"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
__weak ViewController *weakSelf = self;
[createPasswordAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.delegate = weakSelf;
textField.keyboardType = UIKeyboardTypeNumberPad;
}];
UIAlertAction* confirmAction = [UIAlertAction actionWithTitle:@"Confirm"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
self.password = createPasswordAlert.textFields[0].text;
[self confirmPassword];
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self promptPasswordCreation];
}];
[createPasswordAlert addAction:confirmAction];
[createPasswordAlert addAction:cancelAction];
confirmAction.enabled = NO;
[self presentViewController:createPasswordAlert animated:YES completion:nil];
}
果然,在上面的代码中,当输入确认块时警报存在,我可以保存文本 .
通过将块作为参数传递给我的便捷方法,我做了些什么吗?
1 回答
您的一个问题是,当您创建块并将其发送到初始化程序时,您将在块内引用
nil
对象 . 然后使用该nil
引用保存该块,并将其作为块传递给您的处理程序 .在那里
alert
在您创建该块并将其发送以进行保存时尚未初始化 . 修复它的一个选项是使用标准初始值设定项,然后对方法进行分类以添加所需的块函数 . 另一种选择可能是尝试将__block
修饰符添加到alert
对象,尽管我认为这不会有所帮助 .