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

标签 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];
}

最佳答案

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

在@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/

相关文章:

ios - 带有文本字段验证警报的 UIAlertController 在 swift ios 中显示问题

iOS Rich Notifications 多个附件 (iOS 10+)

ios - 用户在 iOS9 上选择 "Allow"后注册本地通知返回 UIUserNotificationTypeNone

javascript - 无法在 javascript 中为音频设置 currentTime(仅限 IOS)

ios - 将应用程序首次打开时的警报设置为弹出/让用户在应用程序密码中设置

ios - 无法停止 NSTimer

ios - 使用XCode构建Sqlite ICU

iOS 8.3 UiAlertView 关闭苹果键盘

objective-c - 使用 NSTimer 以及从常规调用相同的方法传递参数

iphone - 发布 NSTimer iPhone?