2011-09-19 62 views
1

我已经通过uilocalnotifications.so设置了一个闹钟当我的应用程序在后台我的本地通知显示与2按钮关闭和view.when我的应用程序在前台它显示和alertview与2个按钮,停止和贪睡。当单击停止按钮时,我希望我的闹钟应该停止,当点击贪睡按钮时,我希望通知应该在5分钟后出现。这是可能的。请帮助我解决这个概率。谢谢。如何调用按钮上的贪睡功能点击alertview

this is my code : 


//this is my controller class where i am setting my notification 

- (void)clearNotification { 

    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
} 

- (void)scheduleNotification { 

    [reminderText resignFirstResponder]; 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    Class cls = NSClassFromString(@"UILocalNotification"); 
    if (cls != nil) { 

     UILocalNotification *notif = [[cls alloc] init]; 
     notif.fireDate = [datePicker date]; 
     notif.timeZone = [NSTimeZone defaultTimeZone]; 

     notif.alertBody = @"Did you forget something?"; 
     notif.alertAction = @"Show me"; 
     notif.soundName = UILocalNotificationDefaultSoundName; 
     notif.applicationIconBadgeNumber = 1; 

     NSInteger index = [scheduleControl selectedSegmentIndex]; 
     switch (index) { 
      case 1: 
       notif.repeatInterval = NSMinuteCalendarUnit; 
       break; 
      case 2: 
       notif.repeatInterval = NSHourCalendarUnit; 
       break; 
      case 3: 
       notif.repeatInterval = NSDayCalendarUnit; 
       break; 
      case 4: 
       notif.repeatInterval = NSWeekCalendarUnit; 
       break; 
      default: 
       notif.repeatInterval = 0; 
       break; 
     } 

     NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text 
               forKey:kRemindMeNotificationDataKey]; 
     notif.userInfo = userDict; 

     [[UIApplication sharedApplication] scheduleLocalNotification:notif]; 
     [notif release]; 
    } 
} 

#pragma mark - 
#pragma mark === Public Methods === 
#pragma mark - 

- (void)showReminder:(NSString *)text { 

    if (reminderText != nil) { 

     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder" 
                  message:text delegate:nil 
                cancelButtonTitle:nil 
                otherButtonTitles:@"stop",@"Snooze",nil]; 
     [alertView show]; 
     [alertView release]; 



    } 




} 
//this is my appdelegate where i am receiving my notification 
- (void)clearNotification { 

    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
} 

- (void)scheduleNotification { 

    [reminderText resignFirstResponder]; 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    Class cls = NSClassFromString(@"UILocalNotification"); 
    if (cls != nil) { 

     UILocalNotification *notif = [[cls alloc] init]; 
     notif.fireDate = [datePicker date]; 
     notif.timeZone = [NSTimeZone defaultTimeZone]; 

     notif.alertBody = @"Did you forget something?"; 
     notif.alertAction = @"Show me"; 
     notif.soundName = UILocalNotificationDefaultSoundName; 
     notif.applicationIconBadgeNumber = 1; 

     NSInteger index = [scheduleControl selectedSegmentIndex]; 
     switch (index) { 
      case 1: 
       notif.repeatInterval = NSMinuteCalendarUnit; 
       break; 
      case 2: 
       notif.repeatInterval = NSHourCalendarUnit; 
       break; 
      case 3: 
       notif.repeatInterval = NSDayCalendarUnit; 
       break; 
      case 4: 
       notif.repeatInterval = NSWeekCalendarUnit; 
       break; 
      default: 
       notif.repeatInterval = 0; 
       break; 
     } 

     NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text 
               forKey:kRemindMeNotificationDataKey]; 
     notif.userInfo = userDict; 

     [[UIApplication sharedApplication] scheduleLocalNotification:notif]; 
     [notif release]; 
    } 
} 

#pragma mark - 
#pragma mark === Public Methods === 
#pragma mark - 

- (void)showReminder:(NSString *)text { 

    if (reminderText != nil) { 
     /*newtest *new = [[newtest alloc]initWithNibName:@"newtest" bundle:nil]; 
     [self.navigationController pushViewController:new animated:YES]; 
     [new release];*/ 
    // - (void)alertView:(UIAlertView *)alert 
// didDismissWithButtonIndex:(NSInteger) buttonIndex 
//  { 
//   if (buttonIndex == 0) 
//   { 
//    NSLog(@"Cancel Tapped."); 
//   } 
//   else if (buttonIndex == 1) 
//   {  
//    NSLog(@"OK Tapped. Hello World!"); 
//   } 
//  } 

     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder" 
                  message:text delegate:nil 
                cancelButtonTitle:nil 
                otherButtonTitles:@"stop",@"Snooze",nil]; 
     [alertView show]; 
     [alertView release]; 


    }   

}

- (无效)alertView:(UIAlertView中*)alertView clickedButtonAtIndex:(NSInteger的)buttonIndex
{
的NSString *标题= [alertView buttonTitleAtIndex:buttonIndex];

if(buttonIndex == 0) 
{ 
    NSLog(@"Button 1 was selected."); 
} 
else if([title isEqualToString:@"Button 2"]) 
{ 
    NSLog(@"Button 2 was selected."); 
} 

}

//this is my appdelegate where i am receiving notification 
NSString *kRemindMeNotificationDataKey = @"kRemindMeNotificationDataKey"; 

#pragma mark - 
#pragma mark === Application Delegate Methods === 
#pragma mark - 

- (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    Class cls = NSClassFromString(@"UILocalNotification"); 
    if (cls) { 
     UILocalNotification *notification = [launchOptions objectForKey: 
           UIApplicationLaunchOptionsLocalNotificationKey]; 

     if (notification) { 
      NSString *reminderText = [notification.userInfo 
             objectForKey:kRemindMeNotificationDataKey]; 
      [viewController showReminder:reminderText]; 
      /*newtest *new = [[newtest alloc]initWithNibName:@"newtest" bundle:nil]; 
      [window addSubview:new.view];*/ 
     } 
    } 

    application.applicationIconBadgeNumber = 0; 

    [window addSubview:viewController.view]; 
    [window makeKeyAndVisible]; 

    return YES; 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application { 

    application.applicationIconBadgeNumber = 0; 
} 

- (void)application:(UIApplication *)application 
     didReceiveLocalNotification:(UILocalNotification *)notification { 

    // UIApplicationState state = [application applicationState]; 
    // if (state == UIApplicationStateInactive) { 

     // Application was in the background when notification 
     // was delivered. 
    // } 


    application.applicationIconBadgeNumber = 0; 
    NSString *reminderText = [notification.userInfo 
           objectForKey:kRemindMeNotificationDataKey]; 
    [viewController showReminder:reminderText]; 
    NSLog(@"Recieved Notification %@",notification); 
} 

回答

1

当您点击打盹按钮设置另一个本地通知。当你点击贪睡按钮时,你可以调用另一个功能。在这里,你需要花费当前的时间,并且在开火日期中增加5分钟。

当您的通知来了则u参加收到通知委托方法一个警报和警报的按钮,点击ü调用这个函数

点击你的闹钟铃声的点击ü调用这个函数。

-(void)addNewNotification{ 
     UILocalNotification* localNotification = [[UILocalNotification alloc] init];    
     localNotification.alertBody [email protected]"HI" 
     localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:5*60]; 
     localNotification.timeZone = [NSTimeZone localTimeZone]; 
     localNotification.applicationIconBadgeNumber = localNotification.applicationIconBadgeNumber+1; 
     localNotification.soundName = UILocalNotificationDefaultSoundName; 
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
     [localNotification release]; 
     NSLog(@"%@",[[UIApplication sharedApplication] scheduledLocalNotifications]); 
} 
+0

罗恩,我得到完全confused.Please u能提供关于如何调用该加5分钟firedate另一个函数的一些演示代码。 – Rocky

0

我想你应该使用alertview委托方法clickedAtIndex。 然后检查指数是否是你想要的1表示贪睡,然后在5分钟后重复。

感谢

+0

Sabby我试图添加alertview代码。但是,当alertview按钮被点击时,我的断点没有进入alertview委托方法 – Rocky

+0

您可以显示Alert view委托代码吗?这就是问题所在。 – Akshay

+0

@Akshay我编辑了警报视图代理代码 – Rocky