0

這是我AlertView代碼觸發:`UIAlertController`不NSNotification響應

- (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];呼籲,但警報視圖不會彈出。它說:

「警告:嘗試呈現其視圖不在窗口層次結構中!」

我已嘗試添加通知觀測方法:

- (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!。如果您瞭解,請回復。非常感謝提前。

回答

1

在主隊列調度隊列塊內調用initializeAlertControllerForOneButtonWithTitle方法。

所有UI操作應該是主要威脅。

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