`UIAlertController` 没有在 NSNotification 响应中被触发

`UIAlertController` is not triggered in NSNotification response

这是我的 AlertView 代码:

- (void)initializeAlertControllerForOneButtonWithTitle:(NSString *)title withMessage:(NSString *)msg withYesButtonTitle:(NSString *)yesButtonTitle withYesButtonAction:(id)yesButtonAction
{
    UIAlertController * alert = [UIAlertController
                                 alertControllerWithTitle:title
                                 message:msg
                                 preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* yesBtn = [UIAlertAction
                             actionWithTitle:yesButtonTitle
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action) {
                                 if (self.activityIndicator.animating) {
                                     [self.activityIndicator stopAnimating];
                                 }

                                 if ([title isEqualToString:@"Wrong Password!"]) {
                                     self.editTextField.text = @"";
                                     [self.editTextField becomeFirstResponder];
                                 }
                             }];

    [alert addAction:yesBtn];

    [self presentViewController:alert animated:YES completion:nil];
}

我正试图在我的 NSNotificatoin Response 方法中触发此警报。我的 Notification Response 代码:

- (void)receiveSMSVerificationResponse:(NSNotification *)notification
{
    SMSVerificationDigitClassModel *smsVerificationDigitClassModel = [[SMSVerificationDigitClassModel alloc] init];
    smsVerificationDigitClassModel = [notification object];

    if (smsVerificationDigitClassModel.viewControllerName == ViewControllerNameProfileInfoEditViewController) {

        if ([self alreadyRegisteredPhoneNumber:smsVerificationDigitClassModel.phoneNumber] == YES) {
            NSLog(@"jogajog");
            [self initializeAlertControllerForOneButtonWithTitle:@"Already Registered!" withMessage:kAlreadyRegisteredPhoneNumberMSGForChangePhoneNumber withYesButtonTitle:@"Ok" withYesButtonAction:nil];

        } else {
            if ([AdditionalClasses internetConnectionCheck] == YES) {
                self.userModelClass.phone_number = smsVerificationDigitClassModel.phoneNumber;
                [self updateUserModel:self.userModelClass];
            } else {
                [self noInternetConnectionAlert];
            }
        }
        //Check if that phone number is already used
        // udate phone numner in server
        // update phone number in core data
        //[self goToSignUpViewControllerWithPhoneNumber:smsVerificationDigitClassModel.phoneNumber];
    }
}

我从断点开始检查,[self initializeAlertControllerForOneButtonWithTitle:@"Already Registered!" withMessage:kAlreadyRegisteredPhoneNumberMSGForChangePhoneNumber withYesButtonTitle:@"Ok" withYesButtonAction:nil];这一行确实在调用,但是没有弹出警告视图。它说:

"Warning: Attempt to present on whose view is not in the window hierarchy!"

我尝试添加通知观察者方法:

- (void)addNotificationObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveUserModelResponse:) name:@"sendUpdateRequestToServerForPhoneNumberWithUserModel" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveSMSVerificationResponse:) name:@"SMSVerificationForPhoneNumber" object:nil];
}

viewDidLoadviewDidAppear & 在 viewWillAppearremoveObserverdealloc

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"sendUpdateRequestToServerForPhoneNumberWithUserModel" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"SMSVerificationForPhoneNumber" object:nil];
}

但它根本没有显示。那么,如何在这个 viewController 中更改我的 window hierarchy!。如果你明白了,请回复。非常感谢。

在主队列调度队列块中调用 initializeAlertControllerForOneButtonWithTitle 方法。

所有UI行动都应该针对主要威胁。

 dispatch_async(dispatch_get_main_queue(), ^{
        [self initializeAlertControllerForOneButtonWithTitle:@"Already Registered!" withMessage:kAlreadyRegisteredPhoneNumberMSGForChangePhoneNumber withYesButtonTitle:@"Ok" withYesButtonAction:nil];
 });