可以在iOS应用中超时AlertView吗?

时间:2012-12-07 21:59:43

标签: ios uialertview nstimer

我想知道如果AlertView在屏幕上显示一段时间而没有收到用户的任何确认,它是否有可能超时,如果是,如何?有没有办法让AlertView对象与NSTimer对象链接?

我的基本AlertView代码如下:

- (IBAction)showMessage:(id)sender {
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!"
                                                      message:@"This is your first UIAlertview message."
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];
}

5 个答案:

答案 0 :(得分:4)

这就是我在其中一个应用中实现的方式

在@interface内部声明您的对象,以便您可以跟踪它们并添加(如果需要)

@property (nonatomic, strong) UIAlertView *myAlert;
@property (nonatomic, weak) NSTimer *myTimer;

在您需要启动警报的代码中添加以下内容

self.myAlert = [[UIAlertView alloc]initWithTitle:@"TEST" message:@"TEST" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(cancelAlert) userInfo:nil repeats:NO];
[self.myAlert show];

在代码中的某处添加下一个功能以关闭警报并使NSTimer无效

- (void)cancelAlert {
[self.myAlert dismissWithClickedButtonIndex:-1 animated:YES];
}

如果触摸了按钮,请记住使计时器无效。

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
[self.myTimer invalidate];
// Process pressed button
}

可能需要对您的要求进行一些调整。

答案 1 :(得分:1)

是。使用dismissWithClickedButtonIndex:animated:

例如使用dispatch_after块,如下所示:

int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [message dismissWithClickedButtonIndex:message.cancelButtonIndex animated:YES];
});

如果您想使用NSTimer,只需将UIAlertView保存在实例变量中,这样您就可以在计时器方法中访问它。

答案 2 :(得分:1)

您可以为UIAlertView创建一个类别,并添加一个侦听的观察者,如果它被触发,则自行删除:

@implementation UIAlertView (Cancellable)

+ (instancetype)cancellableAlertViewWithTitle:(NSString *)title
                                      message:(NSString *)message
                                     delegate:(id)delegate
                            cancelButtonTitle:(NSString *)cancelButtonTitle
                            otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:delegate
                                              cancelButtonTitle:cancelButtonTitle
                                              otherButtonTitles:nil];
    if (otherButtonTitles != nil)
    {
        va_list args;
        va_start(args, otherButtonTitles);
        for (NSString *buttonTitle = otherButtonTitles; buttonTitle != nil; buttonTitle = va_arg(args, NSString*))
        {
            [alertView addButtonWithTitle:buttonTitle];
        }
        va_end(args);
    }

    [[NSNotificationCenter defaultCenter] addObserver:alertView selector:@selector(removeAlertView:) name:@"AlertsShouldBeCancelledNotification" object:nil];

    return alertView;
}

- (void)removeAlertView:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self dismissWithClickedButtonIndex:-1 animated:YES];
}

@end

然后你可以在主类中创建一个NSTimer并让它在调用选择器时触发通知。

答案 3 :(得分:0)

如果用户及时点击,请使用NSTimer拨打dismissWithClickedButtonIndex:animated:并使其无效。如果用户已经将消息解除,则使用dispatch_after冒险将消息发送到已发布的实例。

答案 4 :(得分:0)

看一下这个答案:dismissing a UIAlertView programmatically。在我看来,使用performSelector:withObject:afterDelay:比构建和拆除计时器要优雅得多。

相关问题