栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用户设置通知时间

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用户设置通知时间

////  ViewController.swift//  Combining Date and Time////  Created by Leonardo Savio Dabus on 08/12/2014.//  Copyright (c) 2014 inDabusiness.com. All rights reserved.//import UIKitclass ViewController: UIViewController {    // IBOutlet goes here    @IBOutlet var myDatePicker: UIDatePicker!    @IBOutlet var mySwitch: UISwitch!    // let = whatever goes here    // var = whatever goes here    var localNotification = UILocalNotification()   // You just need one    var notificationsCounter = 0    // put your functions now    func datePicker() { myDatePicker.datePickerMode = UIDatePickerMode.Date }    func datePickerDefaultDate() { myDatePicker.date = NSDate().xDays(+1)   }    func notificationsOptions()  {        localNotification.timeZone = NSTimeZone.localTimeZone()        localNotification.repeatInterval = .CalendarUnitDay        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)        localNotification.alertAction = "Open App"        localNotification.alertBody = "Here is the seven o'clock notification"        localNotification.soundName = UILocalNotificationDefaultSoundName        localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1        //     you may add arbitrary key-value pairs to this dictionary.        //     However, the keys and values must be valid property-list types        //     if any are not, an exception is raised.        // localNotification.userInfo = [NSObject : AnyObject]?    }    func toggleSwitch(){        if mySwitch.on{ localNotification.fireDate = myDatePicker.date.fireDate  // combined date = picked Date + 7:00am time        } else { localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999)   // will never be fired        }    }    override func viewDidLoad() {        super.viewDidLoad()        datePicker()        datePickerDefaultDate()        notificationsOptions()        // Do any additional setup after loading the view, typically from a nib.    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }    // here is where you place your IBActions    @IBAction func switchPressed(sender: AnyObject) {        toggleSwitch()    }}

在您的项目中创建一个新的Swift Source文件以放置扩展

import Foundationpublic extension NSDate {    func xDays(x:Int) -> NSDate {        return NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: x, toDate: self, options: nil)!    }    var day: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay,fromDate: self).day}    var month:          Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth,         fromDate: self).month         }    var year:Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear,          fromDate: self).year          }    var fireDate: NSDate    { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 7, minute: 0, second: 0, nanosecond: 0)! }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/660417.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号