2012-10-21 29 views
2

我使用UILocalNotification安排UIAlertView,它工作得很好。但是,如果用户在一段时间后(例如1分钟)没有响应通知,我需要“做点什么”。另外,如果还有其他方法可以执行此操作,则不必使用UIAlertViewxcode ios-本地通知与计时器

回答

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 
+0

谢谢,这个解决方案看起来像它应该工作! –