xcode ios-带定时器的本地通知

时间:2012-10-21 14:09:22

标签: ios xcode notifications uilocalnotification

我正在使用UILocalNotification来安排UIAlertView,这项工作正常。但是,如果用户在一段时间(例如1分钟)后没有响应通知,我需要“做某事”。另外,如果有其他方法可以使用UIAlertView,我不必使用它。

1 个答案:

答案 0 :(得分:0)

只要显示UIAlertView,就可以使用NSTimer启动计时器。计时器完成后,您可以执行所需的特定操作。当用户点击UIAlertView中的其中一个按钮时,您将使计时器无效。

快速样本:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UIAlertViewDelegate> {
    UIAlertView *alert;
    NSTimer *timer;
}

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    alert = [[UIAlertView alloc] initWithTitle:@"Test" message:@"test" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];

    timer = [NSTimer timerWithTimeInterval:4 target:self selector:@selector(timerTick:) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

    return YES;
}

- (void)timerTick:(NSTimer*)timer
{
    [alert dismissWithClickedButtonIndex:-1 animated:YES];
}

- (void)alertViewCancel:(UIAlertView *)alertView
{
    [timer invalidate];
}

@end