如何同时处理多个警报

时间:2019-05-16 23:41:43

标签: objective-c uialertcontroller

我有多个同时运行的nsnotificationcenters,它们在触发时会显示警报。有时,这一次会触发多个警报,但是您当然只能显示一个警报,而另一个则不会出现。处理这种情况的最佳方法是什么,以便可以连续发出多个警报。

我已经厌倦了以一种方法来使用警报,并且当一个警报显示时,将另一个通知放入一个数组中,然后在该数组中运行,但这不能正常工作。我也尝试过将警报设置为单独的方法,但这两种方法都不起作用。

我已经看过使用信号量,但是找不到一个很好的例子。

这是我的通知,可以正常运行。我也在寻找有关通知的建议。在viewDidLoad的viewDidAppear中,哪里是添加观察者的最佳位置。无论何时显示警报,viewDidLoad都会发出警告,因为它希望显示在不在层次结构中的视图上。

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // * 26 APR 2019 * 1.0.4.0
    // Add observer for notifications
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"ROC" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"ROP" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"CARGO" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"PAX" object:nil];

}

这是我的选择器方法,对所有警报使用单一方法。我是编码的新手,所以我确信这不是好习惯,因此,任何建议都将不胜感激。如果当前视图是uialertcontroller,我试图将任何其他通知放在数组中,然后在数组中运行并在之后显示这些警报,但这不能满足我的期望。

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // * 26 APR 2019 * 1.0.4.0
    // Add observer for notifications
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"ROC" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"ROP" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"CARGO" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"PAX" object:nil];

}

- (void)receivedNotification:(NSNotification *)notification {
    NSMutableDictionary *msgData = [[FlightDataInput sharedFlightDataInput] dataForPage:4];
    NSMutableArray *alertArray = [[NSMutableArray alloc] init];

    if([self.presentedViewController isKindOfClass:[UIAlertController class]]) {
        [alertArray addObject:notification];
    }

    if(![self.presentedViewController isKindOfClass:[UIAlertController class]] && [alertArray count] == 0) {

        if([notification.name  isEqualToString: @"ROC"]) {

            UIAlertController *alertRoc = [UIAlertController alertControllerWithTitle:[msgData valueForKey:@"rocTitle"] message:[msgData valueForKey:@"rocMsg"] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                [alertRoc dismissViewControllerAnimated:YES completion:nil];
            }];

            [alertRoc addAction:ok];

            [self presentViewController:alertRoc animated:NO completion:nil];
        }
        if ([notification.name isEqualToString:@"ROP"]) {
            UIAlertController *alertRop = [UIAlertController alertControllerWithTitle:[msgData valueForKey:@"ropTitle"] message:[msgData valueForKey:@"ropMsg"] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                [alertRop dismissViewControllerAnimated:YES completion:nil];
            }];

            [alertRop addAction:ok];

            [self presentViewController:alertRop animated:NO completion:nil];
        }
        if ([notification.name isEqualToString:@"CARGO"]) {
            UIAlertController *alertCargo = [UIAlertController alertControllerWithTitle:[msgData valueForKey:@"cargoTitle"] message:[msgData valueForKey:@"cargoMsg"] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                [alertCargo dismissViewControllerAnimated:YES completion:nil];
            }];

            [alertCargo addAction:ok];

            [self presentViewController:alertCargo animated:NO completion:nil];
        }
        if ([notification.name isEqualToString:@"PAX"]) {
            UIAlertController *alertPax = [UIAlertController alertControllerWithTitle:[msgData valueForKey:@"paxTitle"] message:[msgData valueForKey:@"paxMsg"] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                [alertPax dismissViewControllerAnimated:YES completion:nil];
            }];

            [alertPax addAction:ok];

            [self presentViewController:alertPax animated:NO completion:nil];
        }
    }
    if([alertArray count] > 0) {
        for(int i = 0; i < [alertArray count]; i++) {
            // creating the same alerts in here if there are alerts in the array
        }
    }
}

我有多个同时运行的nsnotificationcenters,它们在触发时会显示警报。有时,这一次会触发多个警报,但是您当然只能显示一个警报,而另一个则不会出现。处理这种情况的最佳方法是什么,以便可以连续发出多个警报。

我已经厌倦了以一种方法来使用警报,并且当一个警报显示时,将另一个通知放入一个数组中,然后在该数组中运行,但这不能正常工作。我还尝试过将警报设置为单独的方法,但这也不起作用。

1 个答案:

答案 0 :(得分:2)

您好,您一次只能显示一个警报。如果您想要更多的连锁店,那么首先关闭现有的警报。这是示例,请检查并更新。

{
  NSMutableDictionary *msgData;
  NSMutableArray <NSNotification *> *alertArray;
  int alertIndex;
}

- (void)viewDidLoad:(BOOL)animated {
    [super viewDidLoad:animated];
    // * 26 APR 2019 * 1.0.4.0
    // Add observer for notifications

    msgData = [[FlightDataInput sharedFlightDataInput] dataForPage:4];
    alertArray = [NSMutableArray new];
    alertIndex = 0;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"ROC" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"ROP" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"CARGO" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"PAX" object:nil];
}

- (void)receivedNotification:(NSNotification *)notification {

      [alertArray addObject:notification];
        if(![self isAlertExist]) {
          [self checkAlerts];           
         }
}


-(void) checkAlerts
{
  if(alertIndex < [alertArray count])
  {
    NSNotification *notification = (NSNotification *)[alertArray objectAtIndex:arrayIndex];
    arrayIndex = arrayIndex + 1;

    if([notification.name  isEqualToString: @"ROC"]) {
                [self showAlertWithTitle:[msgData valueForKey:@"rocTitle"] andMessage:[msgData valueForKey:@"rocMsg"]];
        }
        else if ([notification.name isEqualToString:@"ROP"]) {
          [self showAlertWithTitle:[msgData valueForKey:@"ropTitle"] andMessage:[msgData valueForKey:@"ropMsg"]];
        }
      else if ([notification.name isEqualToString:@"CARGO"]) {
          [self showAlertWithTitle:[msgData valueForKey:@"cargoTitle"] andMessage:[msgData valueForKey:@"cargoMsg"]];
        }
        else if ([notification.name isEqualToString:@"PAX"]) {
            [self showAlertWithTitle:[msgData valueForKey:@"paxTitle"] andMessage:[msgData valueForKey:@"paxMsg"]];
        }
  }
}

-(void) showAlertWithTitle:(NSString *)title andMessage:(NSString *)message
{
  UIAlertController *alertPax = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
      [alertPax dismissViewControllerAnimated:YES completion:^{
        [self checkAlerts];
      }];
  }];

  [alertPax addAction:ok];
  [self presentViewController:alertPax animated:NO completion:nil];
}

-(BOOL) isAlertExist {
  for (UIWindow* window in [UIApplication sharedApplication].windows) {
     if ([window.rootViewController.presentedViewController isKindOfClass:[UIAlertController class]]) {
         return YES;
     }
 }
 return NO;
}