0

问题是,如果我创建并显示两个警报 - 第二个将覆盖第一个,并在它关闭后首先显示。所以不漂亮。

我正在尝试使用 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];
4

2 回答 2

0

我将 [_alert show] 移至 -(void)main 方法,它起作用了!谢谢你,@phix23 的帮助!

于 2012-09-08T20:14:34.787 回答
0

让自己更轻松。创建一个可变数组。当您有新的警报要显示时,然后将它们推送到阵列上。每次警报完成时(获取其委托消息),然后将下一个警报分派到主队列:

NSMutableArray *alerts;

... end of Alert Delegate message
if([alert count]) {
  UIAlert *alrt = [alerts objectAtIndex:0];
  [alerts removeObjectAtIndex:0];
  dispatch_async(dispatch_get_main_queue(), ^{ [alrt show]; } );
}
于 2012-09-05T17:46:10.717 回答