objective-c - 如何处理同时触发的多个警报
问题描述
我有多个 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 同时运行,触发时会显示警报。有时,这会导致一次触发多个警报,但当然您只能显示一个而另一个不会出现。处理这种情况的最佳方法是什么,以便可以连续发出多个警报。
我已经厌倦了在一种方法中使用警报,当显示一个警报时,将另一个通知放在一个数组中,然后通过该数组运行,但这不能正常工作。我也尝试过以不同的方法发出警报,但这也不起作用。
解决方案
您好,您一次只能显示一个警报。如果你想要更多的链,那么首先关闭存在的警报。这是样品,请检查并更新。
{
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;
}
推荐阅读
- javascript - 如果未选中,如何验证此选择选项?
- ios - Unwind segue 从视图中删除了原始 VC,为什么?
- php - Yii 2 基本单元测试我新添加的类没有执行测试结果
- python - Python Tornado:实例变量没有从一个方法保存到另一个
- assembly - 我得到一个堆栈溢出,我不太确定如何
- python - 如何通过在 Python 中使用 selenium 单击此地图中的元素
- hadoop - hadoop作业如何在各个节点上运行
- c# - 错误内联键盘电报机器人 vb.net
- python - 使用 pandas Grouper 按十年开始对 DataFrame 进行分组
- php - 如何将 php 表格放入 html 侧边栏?