【问题标题】:how to call the snooze feature on the button click of alertview如何在alertview的按钮单击上调用贪睡功能
【发布时间】:2011-09-19 04:11:16
【问题描述】:

我已经通过 uilocalnotifications 设置了警报。所以当我的应用程序在后台时,我的本地通知会显示 2 个按钮关闭和查看。当我的应用程序在前台时,它会显示和 alertview 有 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);
}

【问题讨论】:

    标签: iphone objective-c


    【解决方案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]);
    }
    

    【讨论】:

    • 罗恩,我完全搞糊涂了。请你提供一些演示代码,说明如何调用另一个函数,这会增加 5 分钟的触发时间。
    【解决方案2】:

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

    谢谢

    【讨论】:

    • Sabby 我尝试添加 alertview 代码。但是当单击 alertview 按钮时,我的断点没有进入 alertview 委托方法
    • 您能显示您的警报视图委托代码吗?这就是问题所在。
    • @Rocky,您在创建警报视图时已将nil 设置为delegate。你应该设置delegate:self
    • @Empty stack 是对的,如果你的委托是 nil,那么它将如何调用你需要的委托,将它发送到当前班级并分配给 selg...
    • 我也犯了这个错误,因为我也复制了相同的代码并且在使用之前没有对其进行修改... :D 你没看错... :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多