Objective c `NSNotification响应中未触发UIAlertController`
这是我的Objective c `NSNotification响应中未触发UIAlertController`,objective-c,nsnotificationcenter,uialertcontroller,nsnotifications,presentviewcontroller,Objective C,Nsnotificationcenter,Uialertcontroller,Nsnotifications,Presentviewcontroller,这是我的AlertView代码: - (void)initializeAlertControllerForOneButtonWithTitle:(NSString *)title withMessage:(NSString *)msg withYesButtonTitle:(NSString *)yesButtonTitle withYesButtonAction:(id)yesButtonAction { UIAlertController * alert = [UIAlertCont
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
方法中触发此警报。我的通知响应
代码:
- (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];
}
}
我从断点开始检查它,这一行[自初始化ErtControllerforoneButtonWithTitle:@“已注册!”并显示消息:KalReadyRegisteredPhoneNumber用于更改电话号码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];
}
在viewdiload
中,viewdilead
中的viewdilead
将出现和removeObserver
中的dealloc
- (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];
}
但它根本没有表现出来。那么,如何更改我的窗口层次结构呢此视图控制器中的code>。如果您理解,请回复。非常感谢。在主队列调度队列块中调用initializeAlertControllerForOneButtonWithTitle方法
所有UI操作都应在主威胁上。
dispatch_async(dispatch_get_main_queue(), ^{
[self initializeAlertControllerForOneButtonWithTitle:@"Already Registered!" withMessage:kAlreadyRegisteredPhoneNumberMSGForChangePhoneNumber withYesButtonTitle:@"Ok" withYesButtonAction:nil];
});