-1

我想显示一个大约 2 秒的临时警报,让用户有机会取消操作。如果用户单击取消按钮,则不会调用后续方法。但是,如果用户没有按取消,则将调用后续方法。有人对如何使这项工作有任何想法吗?

我有一个看起来像这样的设置:

-(void) buttonPress {

    //alert with message and cancel button
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"2 seconds to cancel" 
    message:@"Push Cancel to stop" delegate:self cancelButtonTitle:@"Cancel" 
    otherButtonTitles:nil, nil];
    alert.tag = 1;
    [alert show];
    return;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 0 && alertView.tag == 1) {

        //action cancelled
    }
    else {

        //action not cancelled
    }
}
4

10 回答 10

5

试试这个在 2 秒后关闭警报:

-(void) buttonPress {

    //alert with message and cancel button
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"2 seconds to cancel" 
    message:@"Push Cancel to stop" delegate:self cancelButtonTitle:@"Cancel" 
    otherButtonTitles:nil, nil];
    alert.tag = 1;
    [alert show];

    // Create the perform selector method with alert view object and time-period
    [self performSelector:@selector(dismissAlertView:) withObject:alert afterDelay:2];
    return;
}


// Dismiss the alert view after time-Period     
-(void)dismissAlertView:(UIAlertView *)alert
{
    [alert dismissWithClickedButtonIndex:-1 animated:YES];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 0 && alertView.tag == 1) {

        //action cancelled
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(dismissAlertView:) object:alertView];
    }
    else {

        //action not cancelled
    }
}
于 2013-06-25T07:05:45.043 回答
0

您可以创建一种方法,该方法将在 2 秒后调用并关闭警报视图并执行您想要执行的任务。

您可以按如下方式调用该方法 -

[self performSelector:@selector(yourMethodName) withObject:nil afterDelay:2];
于 2013-06-25T07:02:22.983 回答
0

使用您想要的延迟(2 秒)设置一个 NSTimer。在计时器的委托中,使用适当的参数调用 UIAlertView 的委托。

于 2013-06-25T07:02:35.693 回答
0

NSTimer当您显示警报(非重复)并在委托回调中取消它时开始。如果计时器触发,请关闭警报并调用您的后续任务。

于 2013-06-25T07:03:24.260 回答
0

下面是逻辑..

  1. 大约 2 秒的临时警报,让用户有机会取消操作。

    设置一个标志变量以获取指示天气是否继续。

  2. 如果用户单击取消按钮,则不会调用后续方法。

    UIAlertview 出现在用户面前并将标志设置为 false 时立即启动计时器 2 秒。

  3. 但是,如果用户没有按取消,则将调用后续方法。

    隐藏 UIAlertview,停止 2 秒计时器和标志值将为真并调用您的后续方法以继续您的逻辑。

于 2013-06-25T07:05:23.393 回答
0

如果您的应用程序在显示警报视图后进入后台,这甚至会运行。

UIBackgroundTaskIdentifier bgTask = 0;
    UIApplication  *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
    }];

   NSTimer* currentTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self
                                                  selector:@selector(YourMethodName) userInfo:nil repeats:YES];

享受!!....

于 2013-06-25T11:51:30.140 回答
0

使用此代码:

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(FunctionName) userInfo:nil repeats:NO];
-(Void)YourFunctionName
{
}
于 2013-06-25T07:08:00.760 回答
0

尝试这个:

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(popViewAfterMessage) userInfo:nil repeats:NO];

-(void) popViewAfterMessage {
    // your code here...
}
于 2013-06-25T07:04:37.963 回答
-1

使用这个功能:

- (void)hideAllAlerts {

    for (UIWindow* window in [UIApplication sharedApplication].windows) {
        NSArray* subviews = window.subviews;
        if ([subviews count] > 0)
            if ([[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]])
                [(UIAlertView *)[subviews objectAtIndex:0] dismissWithClickedButtonIndex:[(UIAlertView *)[subviews objectAtIndex:0] cancelButtonIndex] animated:NO];
    }
}

更改您的代码:

-(void) buttonPress {

    //alert with message and cancel button
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"2 seconds to cancel" 
    message:@"Push Cancel to stop" delegate:self cancelButtonTitle:@"Cancel" 
    otherButtonTitles:nil, nil];
    alert.tag = 1;
    [alert show];
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(hideAllAlerts) userInfo:nil repeats:NO];

    return;
}
于 2013-06-25T07:35:27.257 回答
-1
 [self performSelector:@selector(sendMail) withObject:nil afterDelay:1.0];
于 2013-06-25T07:39:10.750 回答