解决非当前页面的UIAlertController在当前页面显示

在A页面中有这个方法,以及网络请求中对它的调用。由于网络请求是被动触发的(极光推送),当应用在B页面时,极光推送出发了A页面中的网络请求,导致A页面中的UIAlertController在B页面中显示。

- (void)alertWithMessage:(NSString *)msg{

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alert addAction:okAction];

[self presentViewController:alert animated:NO completion:nil];

}

解决方法:当A页面不是当前页面时,不让UIAlertController产生;

1、定义标志量,用于标志页面是否在当前

{

BOOL isCurrentPage; //是否在当前页,如果不是,就不要推出UIAlertController

}

2、给标志量赋值,用于区分页面状态

- (void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

isCurrentPage = YES;

}

- (void)viewWillDisappear:(BOOL)animated{

[super viewWillDisappear:animated];

isCurrentPage = NO;

}

3、根据页面状态选择是否推出提示

- (void)alertWithMessage:(NSString *)msg{

if (!isCurrentPage) {

return;

}

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alert addAction:okAction];

[self presentViewController:alert animated:NO completion:nil];

}

你可能感兴趣的:(解决非当前页面的UIAlertController在当前页面显示)