2012-09-05 2 views
0

문제는 내가 두 개의 경고를 만들고 표시 할 때 두 번째 경고가 첫 번째 경고를 무시하고 첫 번째 경고가 닫힌 후에 표시된다는 것입니다. 너무 예뻐요.NSOperaionQueue 및 UIAlertView

NSOperationQueue를 사용하여 대기열 경고를 만들려고합니다. 몇 가지 알림을 추가 할 수 있으며 닫을 순서를 표시합니다. 하지만 나는 그렇게 할 수 없다. 내가 추가 한 작업은 순차적으로 수행되고, 이전 작업을 기다리는 것이다. 그것들은 병행하여 실행됩니다.

AlertOperation.h

#import <Foundation/Foundation.h> 

@interface AlertOperation : NSOperation<UIAlertViewDelegate> 

@property (nonatomic,assign) BOOL isFinishedAlert; 

- (AlertOperation *)initWithAlert:(UIAlertView *)alert; 

@end 

AlertOperation.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]; 
+0

메인에 [_alert show] 넣기 – Felix

+0

예! 고맙습니다, @ phix23! 간단한 논리. – glebus

답변

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]; }); 
} 
0

나는에 [_alert 쇼] 이동 - (무효) 주요 방법과 일했다! 도와 주셔서 감사합니다, @ phix23!