Objective c 按顺序显示UIAlertView

Objective c 按顺序显示UIAlertView,objective-c,uiview,uialertview,alert,Objective C,Uiview,Uialertview,Alert,我有几个UIAlertView,我希望按顺序显示,并且只有在上一个UIAlertView被取消后(通过用户单击“确定”),才能继续显示下一个UIAlertView 我知道didDismissWithButtonIndex委托和添加标记,但这并没有太大帮助,因为可能会调用多达3个UIAlertView,而且每次调用的顺序不一定相同。见代码: if(condition 1){ alert1 = // UIAlertView[[..... [alert1 show] } if(con

我有几个UIAlertView,我希望按顺序显示,并且只有在上一个UIAlertView被取消后(通过用户单击“确定”),才能继续显示下一个UIAlertView

我知道didDismissWithButtonIndex委托和添加标记,但这并没有太大帮助,因为可能会调用多达3个UIAlertView,而且每次调用的顺序不一定相同。见代码:

if(condition 1){
    alert1 = // UIAlertView[[.....
    [alert1 show]
}

if(condition 2){
    alert2 = // UIAlertView[[.....
    [alert2 show]
}

if(condition 3){
    alert3 = // UIAlertView[[.....
    [alert3 show]
}
上面只会在每个警报之上添加3个警报(取决于满足多少条件),这不是我想要的。我希望一次只能显示一个,然后在用户点击ok按钮后显示下一个(如果有)

我有一个想法,可能是将消息添加到队列中,然后在每次警报被解除时处理该队列以删除警报,但我不确定我该怎么做

任何想法都将不胜感激。
谢谢

您可以在UIAlertView委托方法中轻松完成此操作,该方法在警报视图被取消后调用。因此,UIAlertViewDelegate定义了以下委托方法:

– alertView:didDismissWithButtonIndex:
实现该方法,并确保您的类是您创建的UIAlertView的委托。此方法是根据用户解除的警报显示下一个警报的最佳位置

如果您的要求是“按顺序显示最多三个警报,但不总是以相同的顺序显示”,我可能会将警报放入一个数组中,然后在委托方法中从数组中获取下一个警报以显示。没有必要比这更复杂了;关键是委托方法实现是显示下一个警报的最佳位置

伪代码示例:

定义一个数组;NSMUTABLEARRY*警报

- (void)showAlertSequence {
    if ( !alerts_ ) {
        alerts_ = [[NSMutableArray alloc] init];
    }

    [alerts_ addObjects;<My alerts>];

    [self showSequencedAlertFrom:nil];
}

- (BOOL)showSequencedAlertFrom:(UIAlertView *)sourceAlertView {

    if ( !sourceAlertView ) {
        [[alerts_ objectAtIndex:0] show];
    }
    else {
        NSInteger index = [alerts_ indexOfObject:sourceAlertView];

        if ( index < [alerts_ count] ) {
            [[alerts_ objectAtIndex:index++] show];
        }
    }

    return NO;
}

– alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)index {

    // Show the next alert or clean up if we're at the end of the sequence.
    if ( ![self showSequencedAlertFrom:alertView] ) {
        [alerts_ removeAllObjects];
    }
}   
-(void)showAlertSequence{
如果(!警报){
警报\[[NSMutableArray alloc]init];
}
[警报添加对象;];
[self showSequencedAlertFrom:nil];
}
-(BOOL)showSequencedAlertFrom:(UIAlertView*)sourceAlertView{
如果(!sourceAlertView){
[[alerts\uuObjectAtIndex:0]显示];
}
否则{
NSInteger索引=[alerts\uuIndexofObject:sourceAlertView];
如果(索引<[警报计数]){
[[alerts_uuObjectatindex:index++]show];
}
}
返回否;
}
–alertView:(UIAlertView*)alertView DidDismissinButtonIndex:(NSInteger)索引{
//显示下一个警报或清理(如果我们在序列的末尾)。
如果(![self-showSequencedAlertFrom:alertView]){
[警报\删除所有对象];
}
}   

作为旁白;三个连续的警报会让您的用户非常恼火;)

您可以在UIAlertView委托方法中轻松完成此操作,该方法在警报视图被解除后调用。因此,UIAlertViewDelegate定义了以下委托方法:

– alertView:didDismissWithButtonIndex:
实现该方法,并确保您的类是您创建的UIAlertView的委托。此方法是根据用户解除的警报显示下一个警报的最佳位置

如果您的要求是“按顺序显示最多三个警报,但不总是以相同的顺序显示”,我可能会将警报放入一个数组中,然后在委托方法中从数组中获取下一个警报以显示。没有必要比这更复杂了;关键是委托方法实现是显示下一个警报的最佳位置

伪代码示例:

定义一个数组;NSMUTABLEARRY*警报

- (void)showAlertSequence {
    if ( !alerts_ ) {
        alerts_ = [[NSMutableArray alloc] init];
    }

    [alerts_ addObjects;<My alerts>];

    [self showSequencedAlertFrom:nil];
}

- (BOOL)showSequencedAlertFrom:(UIAlertView *)sourceAlertView {

    if ( !sourceAlertView ) {
        [[alerts_ objectAtIndex:0] show];
    }
    else {
        NSInteger index = [alerts_ indexOfObject:sourceAlertView];

        if ( index < [alerts_ count] ) {
            [[alerts_ objectAtIndex:index++] show];
        }
    }

    return NO;
}

– alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)index {

    // Show the next alert or clean up if we're at the end of the sequence.
    if ( ![self showSequencedAlertFrom:alertView] ) {
        [alerts_ removeAllObjects];
    }
}   
-(void)showAlertSequence{
如果(!警报){
警报\[[NSMutableArray alloc]init];
}
[警报添加对象;];
[self showSequencedAlertFrom:nil];
}
-(BOOL)showSequencedAlertFrom:(UIAlertView*)sourceAlertView{
如果(!sourceAlertView){
[[alerts\uuObjectAtIndex:0]显示];
}
否则{
NSInteger索引=[alerts\uuIndexofObject:sourceAlertView];
如果(索引<[警报计数]){
[[alerts_uuObjectatindex:index++]show];
}
}
返回否;
}
–alertView:(UIAlertView*)alertView DidDismissinButtonIndex:(NSInteger)索引{
//显示下一个警报或清理(如果我们在序列的末尾)。
如果(![self-showSequencedAlertFrom:alertView]){
[警报\删除所有对象];
}
}   

作为旁白;三个连续的警报会让您的用户非常恼火;)

我所做的一件事是通过在AlertView上添加一个类别来使用基于块的UIAlertView

这是.h文件

@interface UIAlertView (WithBlocks)

- (id) initWithTitle:(NSString *)title message:(NSString *)message;
- (void) addButtonWithTitle:(NSString *)title andBlock:(void(^)())block;

@end
这是.m文件

static NSString *BUTTON_BLOCK_KEY = @"alertview-button-blocks";

@interface UIAlertView()
- (void) runBlock: (void (^)())block;
@end

@implementation UIAlertView (WithBlocks)

/**
 * Initialized an alert view with a title and message.
 */
- (id) initWithTitle:(NSString *)title message:(NSString *)message
{
    self = [self initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    if (self) {
        self.delegate = self;
        NSMutableArray *buttonBlocks = [NSMutableArray array];
        objc_setAssociatedObject(self, BUTTON_BLOCK_KEY, buttonBlocks, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    }
    return self;
}

/**
 * Adds a button with a title and a block to be executed when that button is tapped.
 */
- (void) addButtonWithTitle:(NSString *)title andBlock:(void (^)())block
{
    // Add the button
    [self addButtonWithTitle:title];
    NSMutableArray *buttonBlocks = objc_getAssociatedObject(self, BUTTON_BLOCK_KEY);
    if (!block) {
        block = ^{ /* empty block */ };
    }
    [buttonBlocks addObject:[[[block copy] retain] autorelease]];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSMutableArray *buttonBlocks = objc_getAssociatedObject(self, BUTTON_BLOCK_KEY);
    void (^block)() = (void (^)()) [buttonBlocks objectAtIndex:buttonIndex];

    // Due to a timing issue, the current window is still the UIAlertView for a very
    // short amount of time after it has been dismissed which messes up anything
    // trying to get the current window in the blocks being run.
    // Ergo, the block is being delayed by a tiny bit. (Amount determined through limited testing)
    [self performSelector:@selector(runBlock:) withObject:block afterDelay:0.25];
}

- (void) runBlock: (void (^)())block
{
    block();
}

@end
然后,您可以通过以下代码调用将AlertView链接在一起

 void(^continueBlock)(void) = ^{
     // Display more alertviews here
 };

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message"];
 [alert addButtonWithTitle:@"Continue" andBlock:continueBlock];

 [alert addButtonWithTitle:@"Dismiss" andBlock:^{
   // Display more alertviews here
 }
 [alert show];
 [alert release];

我所做的一件事是通过在AlertView上添加一个类别来使用基于块的UIAlertView

这是.h文件

@interface UIAlertView (WithBlocks)

- (id) initWithTitle:(NSString *)title message:(NSString *)message;
- (void) addButtonWithTitle:(NSString *)title andBlock:(void(^)())block;

@end
这是.m文件

static NSString *BUTTON_BLOCK_KEY = @"alertview-button-blocks";

@interface UIAlertView()
- (void) runBlock: (void (^)())block;
@end

@implementation UIAlertView (WithBlocks)

/**
 * Initialized an alert view with a title and message.
 */
- (id) initWithTitle:(NSString *)title message:(NSString *)message
{
    self = [self initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    if (self) {
        self.delegate = self;
        NSMutableArray *buttonBlocks = [NSMutableArray array];
        objc_setAssociatedObject(self, BUTTON_BLOCK_KEY, buttonBlocks, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    }
    return self;
}

/**
 * Adds a button with a title and a block to be executed when that button is tapped.
 */
- (void) addButtonWithTitle:(NSString *)title andBlock:(void (^)())block
{
    // Add the button
    [self addButtonWithTitle:title];
    NSMutableArray *buttonBlocks = objc_getAssociatedObject(self, BUTTON_BLOCK_KEY);
    if (!block) {
        block = ^{ /* empty block */ };
    }
    [buttonBlocks addObject:[[[block copy] retain] autorelease]];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSMutableArray *buttonBlocks = objc_getAssociatedObject(self, BUTTON_BLOCK_KEY);
    void (^block)() = (void (^)()) [buttonBlocks objectAtIndex:buttonIndex];

    // Due to a timing issue, the current window is still the UIAlertView for a very
    // short amount of time after it has been dismissed which messes up anything
    // trying to get the current window in the blocks being run.
    // Ergo, the block is being delayed by a tiny bit. (Amount determined through limited testing)
    [self performSelector:@selector(runBlock:) withObject:block afterDelay:0.25];
}

- (void) runBlock: (void (^)())block
{
    block();
}

@end
然后,您可以通过以下代码调用将AlertView链接在一起

 void(^continueBlock)(void) = ^{
     // Display more alertviews here
 };

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message"];
 [alert addButtonWithTitle:@"Continue" andBlock:continueBlock];

 [alert addButtonWithTitle:@"Dismiss" andBlock:^{
   // Display more alertviews here
 }
 [alert show];
 [alert release];

我也在寻找解决这个问题的办法。以下是我为自己的应用程序解决问题的方法:

static BOOL alertShowing = FALSE;

UIAlertView *alert0 = [[UIAlertView alloc] initWithTitle:@"AlertView 0" message:@"This is the first alert" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Yes",@"No", nil];
[alert0 setTag:0];
alertShowing = TRUE;
[alert0 show];

while (alertShowing) { 
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
}

UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"AlertView 1" message:@"This is the second alert" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Yes",@"No", nil];
[alert1 setTag:1];
alertShowing = TRUE;
[alert1 show];

while (alertShowing) { 
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
}

// add some more alerts here for dramatic effect ...
您的按钮处理程序必须在每个退出路径中设置
alertShowing=FALSE'

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    // Deal with handling responses for your different alerts here.
    switch ([alertView tag]) {
        case 0:
            // handler first alert here
            break;
        case 1:
            // handler second alert here
            break;
        default:
            // etc.
            break;
    }

    alertShowing = FALSE;
}

与创建新的运行循环相比,可能有更好的方法来静坐和旋转,并且可能有一些重复的代码可以更好地进行泛化。另一方面,它很简单,不需要大量排队逻辑。我正在使用#define来定义此模式,以避免手动键入,在我的情况下,它工作得很好。

我也在寻找解决此问题的方法。以下是我为自己的应用程序解决问题的方法:

static BOOL alertShowing = FALSE;

UIAlertView *alert0 = [[UIAlertView alloc] initWithTitle:@"AlertView 0" message:@"This is the first alert" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Yes",@"No", nil];
[alert0 setTag:0];
alertShowing = TRUE;
[alert0 show];

while (alertShowing) { 
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
}

UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"AlertView 1" message:@"This is the second alert" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Yes",@"No", nil];
[alert1 setTag:1];
alertShowing = TRUE;
[alert1 show];

while (alertShowing) { 
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
}

// add some more alerts here for dramatic effect ...
您的按钮处理程序必须在每个退出路径中设置
alertShowing=FALSE'

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    // Deal with handling responses for your different alerts here.
    switch ([alertView tag]) {
        case 0:
            // handler first alert here
            break;
        case 1:
            // handler second alert here
            break;
        default:
            // etc.
            break;
    }

    alertShowing = FALSE;
}

与创建新的运行循环相比,可能有更好的方法来静坐和旋转,并且可能有一些重复的代码可以更好地进行泛化。另一方面,它很简单,不需要大量排队逻辑。我正在使用一个#define来定义此模式,以避免手动键入,在我的情况下,它工作得很好。

以下是我如何使用一个