我想显示约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
}
}
答案 0 :(得分: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
}
}
答案 1 :(得分:0)
您可以制作一个在2秒后调用的方法并关闭警报视图并执行您想要执行的任务。
您可以按如下方式调用该方法 -
[self performSelector:@selector(yourMethodName) withObject:nil afterDelay:2];
答案 2 :(得分:0)
使用您想要的延迟设置NSTimer(2秒)。在计时器的委托中,使用适当的参数调用UIAlertView的委托。
答案 3 :(得分:0)
当您显示警报(非重复)时启动NSTimer
并在委托回调中取消它。如果计时器触发,则关闭警报并调用后续任务。
答案 4 :(得分:0)
试试这个:
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(popViewAfterMessage) userInfo:nil repeats:NO];
-(void) popViewAfterMessage {
// your code here...
}
答案 5 :(得分:0)
以下是逻辑..
临时警报约2秒钟,让用户有机会取消操作。
设置标志变量以获取指示天气是否继续。
如果用户单击取消按钮,则不会调用后续方法。
只要UIAlertview出现在用户面前并且设置标志为false,就启动计时器2秒钟。
但是,如果用户没有按下取消,则会调用后续方法。
隐藏UIAlertview,停止2秒计时器&标志值为true,并调用后续方法继续使用逻辑。
答案 6 :(得分:0)
使用此代码:
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(FunctionName) userInfo:nil repeats:NO];
-(Void)YourFunctionName
{
}
答案 7 :(得分: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];
享受!! ....
答案 8 :(得分:-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;
}
答案 9 :(得分:-1)
[self performSelector:@selector(sendMail) withObject:nil afterDelay:1.0];