Ios 如何在显示两个警报之间暂停代码
我有一些iOS代码,我需要能够显示2个背靠背警报。当用户在第一个警报上单击“确定”时,我需要显示第二个警报。因为显示警报不会“暂停”代码,所以我的第二个警报尝试在几乎与第一个警报同时显示的同时显示,结果出现故障(第二个警报不会显示) 我需要的是一种在显示第一个警报时暂停代码的方法。一旦用户在第一个警报上单击“确定”,则可以显示第二个警报 在进入第二个警报之前,等待第一个警报完成的正确方法是什么 这是我的完整代码,以防有帮助。每个警报都由一个条件触发。如果这两种情况都是真的,那么我将点击我描述的背对背警报问题Ios 如何在显示两个警报之间暂停代码,ios,objective-c,alert,pause,Ios,Objective C,Alert,Pause,我有一些iOS代码,我需要能够显示2个背靠背警报。当用户在第一个警报上单击“确定”时,我需要显示第二个警报。因为显示警报不会“暂停”代码,所以我的第二个警报尝试在几乎与第一个警报同时显示的同时显示,结果出现故障(第二个警报不会显示) 我需要的是一种在显示第一个警报时暂停代码的方法。一旦用户在第一个警报上单击“确定”,则可以显示第二个警报 在进入第二个警报之前,等待第一个警报完成的正确方法是什么 这是我的完整代码,以防有帮助。每个警报都由一个条件触发。如果这两种情况都是真的,那么我将点击我描述的背
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];
}
这样,当您点击“确定”时,它将调用第二个警报的代码。您所要做的只是处理程序中第一个警报的第二个警报:
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”时,它将调用第二个警报的代码。您可以在@界面中设置一个变量,并在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];
您可以在@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];
谢谢你的建议。我花了一段时间才意识到,您正在将第二个警报放入一个块中,并传递到第一个警报中。不过,我需要的是一种干净的方式来处理显示警报a、警报B或警报a和B。如果不需要警报a,我需要添加更多代码来处理警报B。我想我可以这样做。@Morocco5,是的,您必须包含一个条件来检查何时调用它。这就是一次执行两个警报的方式。如果这回答了你的问题,请将其标记为正确,并投票表决。谢谢你的建议。我花了一段时间才意识到,您正在将第二个警报放入一个块中,并传递到第一个警报中。不过,我需要的是一种干净的方式来处理显示警报a、警报B或警报a和B。如果不需要警报a,我需要添加更多代码来处理警报B。我想我可以这样做。@Morocco5,是的,您必须包含一个条件来检查何时调用它。这就是一次执行两个警报的方式。如果这回答了您的问题,请将其标记为正确并进行后续投票。现在我的代码正在运行,请添加后续注释。据我所知,没有办法“暂停”代码以等待用户在警报中点击OK。我曾经尝试过这样做——当用户点击OK时设置一个标志,然后等待该标志被设置,然后再继续。这个等待循环使我的UI崩溃。最后的解决方案是检查是否需要显示2个警报,如果是这种情况,则在传递到第一个警报(下面的解决方案1)的代码块中显示第二个警报,并在我的代码正常工作后添加后续注释。据我所知,没有办法“暂停”代码以等待用户在警报中点击OK。我曾经尝试过这样做——当用户点击OK时设置一个标志,然后等待该标志被设置,然后再继续。这个等待循环使我的UI崩溃。最后的解决方案是检查是否需要显示2个警报,如果是,则在传递到第一个警报的代码块中显示第二个警报(下面的解决方案1)