如何在显示 2 个警报之间暂停代码
How to pause code between the display of 2 alerts
我有一些 iOS 代码需要能够显示 2 个背靠背警报。当用户在第一个警报上单击 "ok" 时,我需要显示第二个警报。因为显示警报不会 "pause" 代码,所以我的第二个警报试图与我的第一个警报几乎同时显示,但事情中断了(第二个警报没有显示)。
我需要的是一种在显示第一个警报时暂停代码的方法。一旦用户在第一个警报上单击 "ok",它就可以显示第二个警报。
在进入第二个警报之前等待第一个警报完成的正确方法是什么?
这是我的完整代码,希望对您有帮助。每个警报都由一个条件触发。如果这两个条件都成立,那么我就会遇到我所描述的背靠背警报问题。
UIAlertController* alert;
UIAlertAction* defaultAction;
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if(status != kCLAuthorizationStatusAuthorizedAlways) {
alert = [UIAlertController alertControllerWithTitle:@"Warning"
message:@"Reminder will not work unless you enable 'Always' location."
preferredStyle:UIAlertControllerStyleAlert];
defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if(settings.types==0) {
alert = [UIAlertController alertControllerWithTitle:@"Warning"
message:@"Reminder will not work unless you enable notifications."
preferredStyle:UIAlertControllerStyleAlert];
defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
您所要做的只是处理程序中的第二个警报:
UIAlertController* alert;
UIAlertAction* defaultAction;
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if(status != kCLAuthorizationStatusAuthorizedAlways) {
alert = [UIAlertController alertControllerWithTitle:@"Warning"
message:@"Reminder will not work unless you enable 'Always' location."
preferredStyle:UIAlertControllerStyleAlert];
defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if(settings.types==0) {
inner_alert = [UIAlertController alertControllerWithTitle:@"Warning"
message:@"Reminder will not work unless you enable notifications."
preferredStyle:UIAlertControllerStyleAlert];
inner_defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[inner_alert addAction:inner_defaultAction];
[self presentViewController:inner_alert animated:YES completion:nil];
}
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
这样,当您点击 "Ok" 时,它会调用第二次警报的代码。
可以在@interface中设置变量,在viewDidLoad中设置为0
@interface ViewController ()
@property (nonatomic) int number;
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.number = 0;
}
然后创建显示警报的函数并通过开关调用其他警报(self.number),或者您可以再次调用 showAlert 本身。
- (void) showAlert {
UIAlertController *alert = [UIAlertController alertController WithTitle:@"ALERT" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
switch (self.number) {
case 0:
self.number = 1;
[self secondAlert]; // other alert function
break;
case 1:
[self thirdAlert];
default:
break;
}
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"CANCEL" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:okAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
我有一些 iOS 代码需要能够显示 2 个背靠背警报。当用户在第一个警报上单击 "ok" 时,我需要显示第二个警报。因为显示警报不会 "pause" 代码,所以我的第二个警报试图与我的第一个警报几乎同时显示,但事情中断了(第二个警报没有显示)。
我需要的是一种在显示第一个警报时暂停代码的方法。一旦用户在第一个警报上单击 "ok",它就可以显示第二个警报。
在进入第二个警报之前等待第一个警报完成的正确方法是什么?
这是我的完整代码,希望对您有帮助。每个警报都由一个条件触发。如果这两个条件都成立,那么我就会遇到我所描述的背靠背警报问题。
UIAlertController* alert;
UIAlertAction* defaultAction;
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if(status != kCLAuthorizationStatusAuthorizedAlways) {
alert = [UIAlertController alertControllerWithTitle:@"Warning"
message:@"Reminder will not work unless you enable 'Always' location."
preferredStyle:UIAlertControllerStyleAlert];
defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if(settings.types==0) {
alert = [UIAlertController alertControllerWithTitle:@"Warning"
message:@"Reminder will not work unless you enable notifications."
preferredStyle:UIAlertControllerStyleAlert];
defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
您所要做的只是处理程序中的第二个警报:
UIAlertController* alert;
UIAlertAction* defaultAction;
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if(status != kCLAuthorizationStatusAuthorizedAlways) {
alert = [UIAlertController alertControllerWithTitle:@"Warning"
message:@"Reminder will not work unless you enable 'Always' location."
preferredStyle:UIAlertControllerStyleAlert];
defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if(settings.types==0) {
inner_alert = [UIAlertController alertControllerWithTitle:@"Warning"
message:@"Reminder will not work unless you enable notifications."
preferredStyle:UIAlertControllerStyleAlert];
inner_defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[inner_alert addAction:inner_defaultAction];
[self presentViewController:inner_alert animated:YES completion:nil];
}
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
这样,当您点击 "Ok" 时,它会调用第二次警报的代码。
可以在@interface中设置变量,在viewDidLoad中设置为0
@interface ViewController ()
@property (nonatomic) int number;
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.number = 0;
}
然后创建显示警报的函数并通过开关调用其他警报(self.number),或者您可以再次调用 showAlert 本身。
- (void) showAlert {
UIAlertController *alert = [UIAlertController alertController WithTitle:@"ALERT" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
switch (self.number) {
case 0:
self.number = 1;
[self secondAlert]; // other alert function
break;
case 1:
[self thirdAlert];
default:
break;
}
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"CANCEL" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:okAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];