如何在alertview按钮上调用贪睡功能

时间:2011-09-19 04:11:17

标签: iphone objective-c

我已经通过uilocalnotifications设置了警报。因此,当我的应用程序处于后台时,我的本地通知会显示2个按钮关闭并查看。当我的应用程序处于前台时,它会显示并且警报视图有2个按钮,停止并打盹。当点击停止按钮我希望我的闹钟应该停止,当点击按下按钮时我想要通知应该在5分钟后出现。这怎么可能。请帮我解决这个问题。谢谢。

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];


    }           

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString * title = [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);
}

2 个答案:

答案 0 :(得分:1)

点击暂停按钮后,您可以设置另一个本地通知。当您单击“暂停”按钮时,您将调用另一个功能。在此,您需要花费当前时间,在火灾日期,您需要添加5分钟。

当您的通知到来时,您会在收到的通知委托方法中点击一个警报,然后点击提醒按钮即可调用此功能

点击你的贪睡提示点击你调用此功能。

-(void)addNewNotification{
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];               
        localNotification.alertBody =@"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]);
}

答案 1 :(得分:0)

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

由于