加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

打开和关闭警报ios

发布时间:2020-12-14 17:59:08 所属栏目:百科 来源:网络整理
导读:我准备了一个闹钟应用程序,它使用UILocalnotification来安排闹钟.现在闹钟设置后,我想做一个开关,这样我就可以用UISwitch打开和关闭它.我只是想不知道怎么办?我现在想的是,当你关闭闹钟时,我会在取消UILocalnotification之前存储DATE和TIME值,这样当用户再
我准备了一个闹钟应用程序,它使用UILocalnotification来安排闹钟.现在闹钟设置后,我想做一个开关,这样我就可以用UISwitch打开和关闭它.我只是想不知道怎么办?我现在想的是,当你关闭闹钟时,我会在取消UILocalnotification之前存储DATE和TIME值,这样当用户再次打开闹钟时我会用存储的DATE和TIME值重新安排它.这是正确的做法,还是有其他方法可以做到这一点?

解决方法

只需使数据库表具有’date’,’isCanceled’字段和唯一id’alarmId’列(使用任何你想要的休息).所以当用户想要取消闹钟时试试这个,

NSString *alarmId = @"some_id_to_cancel"; 
    UILocalNotification *notificationToCancel=nil;            
    for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        if([aNotif.userInfo objectForKey:@"ID"] isEqualToString:alarmId]) { 
            notificationToCancel = aNotif; 
            break; 
        } 
    } 
    [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];

为了更好地使用它,你可以通过

UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 

 if (localNotif == nil)  
  return;

 localNotif.fireDate = itemDate; 
 localNotif.timeZone = [NSTimeZone defaultTimeZone];
 localNotif.alertAction = NSLocalizedString(@"View Details",nil); 
 localNotif.alertBody = title;
 localNotif.soundName = UILocalNotificationDefaultSoundName; 

 NSDictionary *infoDict = [NSDictionary dictionaryWithObject:stringID forKey:@"ID"]; 
 localNotif.userInfo = infoDict; 

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
 [localNotif release];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读