问题是,如果我创建并显示两个警报 - 第二个将覆盖第一个,并在它关闭后首先显示。所以不漂亮。
我正在尝试使用 NSOperationQueue 创建队列警报。您可以添加一些警报,它们会显示要关闭的序列。但我不能这样做会是我添加的操作是按顺序执行的,等待前一个。它们是并行执行的。
警报操作.h
#import <Foundation/Foundation.h>
@interface AlertOperation : NSOperation<UIAlertViewDelegate>
@property (nonatomic,assign) BOOL isFinishedAlert;
- (AlertOperation *)initWithAlert:(UIAlertView *)alert;
@end
警报操作.m
#import "AlertOperation.h"
@interface AlertOperation()
{
UIAlertView *_alert;
}
@end
@implementation AlertOperation
@synthesize isFinishedAlert = _isFinishedAlert;
- (AlertOperation *)initWithAlert:(UIAlertView *)alert
{
self = [super init];
if (self)
{
_alert = alert;
_alert.delegate = self;
[_alert show];
}
return self;
}
- (void) main
{
_isFinishedAlert = NO;
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!_isFinishedAlert);
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
_isFinishedAlert = YES;
}
- (BOOL) isConcurrent
{
return NO;
}
@end
这是运行代码
UIAlertView *u1 = [[UIAlertView alloc] initWithTitle:@""
message:@"Hello i am first alert" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
UIAlertView *u2 = [[UIAlertView alloc] initWithTitle:@""
message:@"Hello i am second alert" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
NSOperation *alertOp1 = [[AlertOperation alloc] initWithAlert:u1];
NSOperation *alertOp2 = [[AlertOperation alloc] initWithAlert:u2];
alertsQueue = [[NSOperationQueue alloc] init];
[alertsQueue setMaxConcurrentOperationCount:1];
[alertsQueue addOperation:alertOp1];
[alertsQueue addOperation:alertOp2];