我想知道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];
}
最佳答案
这就是我在其中一个应用中实现的方式
在@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
}
您可能需要对其进行一些调整。
关于ios - 可以在iOS应用中使AlertView超时吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13771668/