1

我希望仅在用户第一次打开我的应用程序时才显示两个警报视图——第二个在第一个被关闭后出现。我将它设置为仅在以前未显示 UIAlertViews 时才显示它,我不需要帮助。在这种情况下,我需要帮助弄清楚如何连续显示两个警报视图。

-(void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 对我不起作用。

这是我的代码 - 请记住这是在 didFinishLaunchingWithOptions 中:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL didFirstLaunch = [defaults boolForKey:@"DidFirstLaunch"];
if (!didFirstLaunch) {
    [defaults setBool:YES forKey:@"DidFirstLaunch"];

    UIAlertView *successAlert = //not important
    [successAlert show];
    [successAlert release];

    //Somehow show second alert after the first is dismissed
}
4

3 回答 3

4

我将使用GCD&blocks发布一个非常简单的解决方案(GCD 部分是为了以防万一警报视图是在另一个线程上创建的,然后是主线程,回调应该可以安全地在主线程上执行)。请记住,我只是在 5 分钟内编写了此代码,因此您绝对应该努力改进代码。有点难看的一件事是在我的子类中覆盖的委托参数。子类的接口可以稍微改变一下,以使发生的事情更加明显......

无论如何,这里...

首先创建一个 的子类UIAlertView,让它看起来有点像下面...

@interface FSAlertView () <UIAlertViewDelegate>

@property (nonatomic, copy) void (^dismissHandler)(NSInteger buttonIndex);

@end


@implementation FSAlertView

@synthesize dismissHandler = _dismissHandler;

- (void)showWithDismissHandler:(void (^)(NSInteger buttonIndex))dismissHandler
{
    self.dismissHandler = dismissHandler;

    self.delegate = self;

    [self show];
}

// Alert view delegate

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    dispatch_async(dispatch_get_main_queue(), ^ {

        if (_dismissHandler)
        {
            _dismissHandler(buttonIndex);
        }

    });
}

现在在应用程序中,我们可以创建警报视图,如下所示...

FSAlertView *alert1 = [[FSAlertView alloc] initWithTitle:@"Alert 1"
                                                 message:@"Some message"
                                                delegate:nil
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"Show 2nd Alert", nil];

[alert1 showWithDismissHandler:^ (NSInteger buttonIndex) {

    NSLog(@"button pressed: %d", buttonIndex);

    if (buttonIndex == 1)
    {
        UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Alert 2"
                                                         message:@"Hi!"
                                                        delegate:nil
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
        [alert2 show];
    }

}];
于 2012-07-31T12:30:49.087 回答
1

如果我正确理解您的问题,那么这可能会有所帮助:

 UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:@"Alert 1" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [firstAlert show];
    [self performSelector:@selector(test:) withObject:firstAlert afterDelay:2];
    [firstAlert release];

    UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:@"Alert 2" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [secondAlert show];
    [self performSelector:@selector(test:) withObject:secondAlert afterDelay:2];
    [secondAlert release];


-(void)test:(UIAlertView*)alert{
    [alert dismissWithClickedButtonIndex:-1 animated:YES];
}

这将一个接一个地显示两个警报视图。

注意:我不确定您是否使用取消按钮关闭警报,所以我会在几秒钟后自动关闭它们。

于 2012-07-31T12:39:04.530 回答
0

试试这个:

UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[firstAlert setTag:444];
[firstAlert show];
firstAlert = nil;

AlertView 委托方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    switch (alertView.tag) {
        case 444:
        {
            //Cancel ButtonIndex = 0
            if (buttonIndex == 1) {
                UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:@"Title 2" message:@"Message2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Dismiss", nil];
                [secondAlert setTag:555];
                [secondAlert show];
                secondAlert = nil;
            } 
        }
        break;
        case 555:
        {
            if (buttonIndex == 1) {
                NSLog(@"Code Here");
            }
        }
        break;
    }
}
于 2012-07-31T13:09:45.667 回答