处理程序块执行时,UIAlertController为nil

时间:2016-01-04 19:28:51

标签: ios objective-c iphone objective-c-blocks uialertcontroller

预先遇到问题:我有一个有文本字段的UIAlertController。当用户触摸警报中的“确认”按钮时,我想将该文本字段的内容保存为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];
}

现在重申上下文有问题:在行中输入操作块时:

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 个答案:

答案 0 :(得分:0)

您的一个问题是,当您创建块并将其发送到初始化程序时,您将在块内引用nil对象。然后使用该nil引用保存该块,并将其作为块传递给您的处理程序。

UIAlertController *alert = [UIAlertController passcodeCreationAlertWithConfirmBehavior:^{
    firstPasscode = alert.textFields[0].text;
    [self presentConfirmPasscodeAlert];
} andCancelBehavior:^{
    [self presentEnablePasscodeAlert];
}];

在那里alert尚未初始化,但是当您创建该块并将其发送以进行保存时。修复它的一个选项是使用标准初始值设定项,然后对方法进行分类以添加所需的块函数。另一种选择可能是尝试将__block修饰符添加到alert对象,但我认为这不会有帮助。