【发布时间】:2018-08-27 08:16:52
【问题描述】:
当我的应用进入后台时,我想通过单击“取消”按钮来关闭我的 UIAlertController。
我已经设置了后台通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
然后在我的 appDidEnterBackground 函数中我有:
- (void)appDidEnterBackground {
if (_alertController) {
UIAlertAction *cancelAction = [[_alertController actions] objectAtIndex:0];
//HERE: is there a way to trigger the cancelAction??
[_alertController dismissViewControllerAnimated:NO completion:nil];
}
}
我正在努力解决的是如何以编程方式触发 UIAlertAction。这可能吗?
【问题讨论】:
-
我认为您不能以编程方式触发您的
UIAlertAction的按下。您可以尝试复制代码,或者将其分支到全局声明的方法中? -
@MCKapur 您的第二点很有潜力。我已经为 UIAlertController (/UIAlertView for iOS7) 使用了单例,因此这些操作已经在全局范围内定义。如何在全局范围内存储一个块,以便在应用程序进入后台时调用它?
-
@MCKapur 破解了它——我在我的单例中添加了一个 var 来保存完成块操作。然后我打电话给
appDidEnterBackground。如果您作为答案发布,我会接受。 -
太棒了,很高兴听到!没关系 - 我不在乎,只要你解决了你的问题。