Ios 如何一个接一个地呈现多个UIAlertController?
在forin循环中,我需要呈现一个UIAlertController,并等待用户确认后再呈现下一个。我在forin循环中展示了它们,但只有第一个出现(在确认后,其他的不会出现)。任何帮助都将不胜感激 按下按钮时,您可以使用UIAlertController代理显示下一个警报 制作全局警报索引:Ios 如何一个接一个地呈现多个UIAlertController?,ios,objective-c,uialertcontroller,for-in-loop,Ios,Objective C,Uialertcontroller,For In Loop,在forin循环中,我需要呈现一个UIAlertController,并等待用户确认后再呈现下一个。我在forin循环中展示了它们,但只有第一个出现(在确认后,其他的不会出现)。任何帮助都将不胜感激 按下按钮时,您可以使用UIAlertController代理显示下一个警报 制作全局警报索引: NSUInteger alertIndex = 0; 在NSDictionary中使用您的警报详细信息制作全局NSArray,例如: self.alerts = @[@{@"title":@"Aler
NSUInteger alertIndex = 0;
在NSDictionary中使用您的警报详细信息制作全局NSArray,例如:
self.alerts = @[@{@"title":@"Alert", @"message":@"Message 1"}, @{@"title":@"Alert", @"message":@"Message 2"}];
使用索引调用您的第一个警报,例如:
...title:self.alerts[alertIndex][@"title"]...
及
在警报控制器代理的DIDClickButtonIndex中:
alertIndex++;
// call your next alert here, explained above.
我需要做一些类似于在我的应用程序中向用户显示提示的事情。 刚刚设法拼凑起来,它的工作相当好,很容易适应和添加到
/////
// Alert Properties in @interface
////
@property NSOperationQueue *queue;
@property NSMutableArray* operationArray;
@property int operationCounter;
@property int operationTotal;
/////
// Alert Properties End
////
/////////
// Multi Alert Code Start
////////
- (void) alertCodeWithTitleString: (NSString*) titlestring AndMessageString:(NSString*)messagestring {
NSOperation *tempoperation = [NSBlockOperation blockOperationWithBlock: ^(void) {
NSString* tutTitleString = titlestring;
NSString* tutMessageString = messagestring;
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:tutTitleString
message:tutMessageString
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* dontshow = [UIAlertAction
actionWithTitle:@"Don't Show This Again"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
self.operationCounter++;
[self alertOperationCallMethod2];
}];
UIAlertAction* done = [UIAlertAction
actionWithTitle:@"Close"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
self.operationCounter++;
[self alertOperationCallMethod2];
}];
[alert addAction:done];
[alert addAction:dontshow];
[self presentViewController:alert animated:YES completion:nil];
});
} ];
self.operationTotal++;
[self.operationArray addObject:tempoperation];
}
-(void) alertOperationCallMethod1 {
self.operationCounter = 0;
self.operationTotal = 0;
self.queue = [[NSOperationQueue alloc] init];
self.operationArray = [[NSMutableArray alloc] init];
[self alertCodeWithTitleString:@"Title1" AndMessageString:@"Message1"];
[self alertCodeWithTitleString:@"Title2" AndMessageString:@"Message2"];
[self alertCodeWithTitleString:@"Title3" AndMessageString:@"Message3"];
// Just keep adding method calls here to add alerts
[self alertOperationCallMethod2];
}
-(void) alertOperationCallMethod2 {
if (self.operationCounter<self.operationTotal) {
[self.queue addOperation:self.operationArray[self.operationCounter]];
}
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//something here
[self alertOperationCallMethod1];
}
/////////
// Multi Alert Code End
////////
----
/////
//@interface中的警报属性
////
@属性NSOperationQueue*队列;
@属性NSMutableArray*运算符数组;
@属性int操作计数器;
@属性int操作总计;
/////
//警报属性结束
////
/////////
//多警报代码启动
////////
-(void)alertCodeWithTitleString:(NSString*)titlestring和messagestring:(NSString*)messagestring{
NSOperation*tempoperation=[NSBlockOperation blockOperationWithBlock:^(无效){
NSString*TUTTILESTRING=标题字符串;
NSString*tutMessageString=messagestring;
dispatch\u async(dispatch\u get\u main\u queue()^{
UIAlertController*警报=[UIAlertController]
alertControllerWithTitle:TuttTitleString
消息:tutMessageString
首选样式:UIAlertControllerStyleAlert];
UIAlertAction*dontshow=[UIAlertAction
actionWithTitle:@“不再显示此内容”
样式:UIAlertActionStyleDefault
处理程序:^(UIAlertAction*操作)
{
[警报解除ViewControllerInitiated:是完成:无];
自身操作计数器++;
[self-alertOperationCallMethod2];
}];
UIAlertAction*完成=[UIAlertAction]
标题为@“关闭”的操作
样式:UIAlertActionStyleDefault
处理程序:^(UIAlertAction*操作)
{
[警报解除ViewControllerInitiated:是完成:无];
自身操作计数器++;
[self-alertOperationCallMethod2];
}];
[警报添加操作:完成];
[警报添加操作:dontshow];
[自我呈现视图控制器:警报动画:是完成:无];
});
} ];
self.operationTotal++;
[self.operationArray addObject:tempoperation];
}
-(无效)警报操作调用方法1{
自操作计数器=0;
自运行总计=0;
self.queue=[[NSOperationQueue alloc]init];
self.operationArray=[[NSMutableArray alloc]init];
[self-alertCodeWithTitleString:@“Title1”和MessageString:@“Message1”];
[self-alertCodeWithTitleString:@“Title2”和MessageString:@“Message2”];
[self-alertCodeWithTitleString:@“Title3”和MessageString:@“Message3”];
//只需继续在此处添加方法调用即可添加警报
[self-alertOperationCallMethod2];
}
-(无效)警报操作调用方法2{
如果(self.operationCounterUIViewController扩展符合您的要求:
警报排队,先进先出。即后一个警报等待并直到
用户响应以前的警报
为什么要进行循环?正如您所说,您必须等待用户确认,因此在Alertcontroller的按钮操作上创建一个新的警报,问题是每个警报在用户确认时执行一项任务(UIAlertControllerStyleActionSheet,带多个选项)。我正在尝试使用用户选择的不同选项对多个对象执行一项任务。这真的是最好的用户体验吗?正如您在下面的评论中所说,如果您有100个对象怎么办?您真的要让用户确认100个单独的警报吗?那将是糟糕的。@rmaddy这只是最坏情况的一个例子,通常不会发生ppens。我想做的是让每个对象执行一个动作,这取决于在UIAlertController的动作表样式中选择了哪个选项。我在uitableview中选择对象,然后将它们放入一个数组中。有没有更好的方法来循环数组并为用户提供每个对象的多项选择?会发生什么如果我有100个对象?我正在为我的UIAlertController使用UIAlertControllerySytLeactionSheet,每个对象都可以显示不同的选项供用户选择。不要将任何对象设置为全局对象。也许你是指实例变量或属性?这是可能的,只会更复杂,你需要以不同的方式处理每个对象。我从未见过像这样的事情之前,我怀疑苹果会批准。为什么不做5个视图控制器和20个分段按钮或者类似的东西呢?是的@rmaddy,如果你愿意,你可以编辑:)
/////
// Alert Properties in @interface
////
@property NSOperationQueue *queue;
@property NSMutableArray* operationArray;
@property int operationCounter;
@property int operationTotal;
/////
// Alert Properties End
////
/////////
// Multi Alert Code Start
////////
- (void) alertCodeWithTitleString: (NSString*) titlestring AndMessageString:(NSString*)messagestring {
NSOperation *tempoperation = [NSBlockOperation blockOperationWithBlock: ^(void) {
NSString* tutTitleString = titlestring;
NSString* tutMessageString = messagestring;
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:tutTitleString
message:tutMessageString
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* dontshow = [UIAlertAction
actionWithTitle:@"Don't Show This Again"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
self.operationCounter++;
[self alertOperationCallMethod2];
}];
UIAlertAction* done = [UIAlertAction
actionWithTitle:@"Close"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
self.operationCounter++;
[self alertOperationCallMethod2];
}];
[alert addAction:done];
[alert addAction:dontshow];
[self presentViewController:alert animated:YES completion:nil];
});
} ];
self.operationTotal++;
[self.operationArray addObject:tempoperation];
}
-(void) alertOperationCallMethod1 {
self.operationCounter = 0;
self.operationTotal = 0;
self.queue = [[NSOperationQueue alloc] init];
self.operationArray = [[NSMutableArray alloc] init];
[self alertCodeWithTitleString:@"Title1" AndMessageString:@"Message1"];
[self alertCodeWithTitleString:@"Title2" AndMessageString:@"Message2"];
[self alertCodeWithTitleString:@"Title3" AndMessageString:@"Message3"];
// Just keep adding method calls here to add alerts
[self alertOperationCallMethod2];
}
-(void) alertOperationCallMethod2 {
if (self.operationCounter<self.operationTotal) {
[self.queue addOperation:self.operationArray[self.operationCounter]];
}
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//something here
[self alertOperationCallMethod1];
}
/////////
// Multi Alert Code End
////////
----