ios - UIAlertView 当没有按钮按下时计数 10 秒并执行某些操作

标签 ios objective-c uialertview nstimer

在我的UIAlertView我有两个按钮是,否。如果我按Yes做某事,如果我按 No取消UIAlertView所以我可以做到这一点,但我的问题是,如果我不按任何按钮,它就会计数 10 seconds并做点什么?

有什么方法吗?

最佳答案

检查一下这个方法是否有效。将NSTimer类的定时器作为全局变量。

when you click on alert button at that time you must be invalidate the timer.

在.h文件中这样声明

    NSTimer *timer;
    UIAlertView *myalert;

    myalert = [[UIAlertView alloc] initWithTitle:@"alert"message:@"hi" 
                                        delegate:self cancelButtonTitle:@"no"
                               otherButtonTitles:@"yes", nil];
    [myalert show];

   timer= [NSTimer scheduledTimerWithTimeInterval:4.0 
                                           target:self selector:@selector(test) 
                                         userInfo:nil
                                          repeats:NO];

    -(void)test{
        [myalert dismissWithClickedButtonIndex:-1 animated:YES];
        //do somthing
    }
    - (void)alertView:(UIAlertView *)alertView 
        clickedButtonAtIndex:(NSInteger)buttonIndex{
        if(timer!=nil){
        [timer invalidate];
        timer=nil;
        }
    }

关于ios - UIAlertView 当没有按钮按下时计数 10 秒并执行某些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16746817/

相关文章:

ios - CoreData 父子上下文冲突管理

ios - RestKit 自定义响应映射

ios - 在 Restkit 中映射单个 JSON 对象时出错

ios - 无法在按钮上加载 UIImage

iphone - 如何访问 applicationDidBecomeActive 上的远程推送通知数据?

ios - 如何检查用户是否是第一次访问我的 iOS 应用程序?

ios - 设置 UIAlertView 委托(delegate)

objective-c - UIAlertView 仅在关闭后才显示

ios - UIAlert 不显示

ios - 为什么 Y 位置为 0 的 UILabel 不位于屏幕顶部