在 Swift 中设置多次通知
Posted
技术标签:
【中文标题】在 Swift 中设置多次通知【英文标题】:Setting Multiple Times for Notifications in Swift 【发布时间】:2014-12-08 20:16:44 【问题描述】:所以我现在正在使用 Swift,当一个开关被激活时,我在某个时间出现了一个通知。
但是,我希望在另一个开关被激活时再发出另一个通知。
这是我的 ViewController 代码:
@IBOutlet var myDatePicker: UIDatePicker!
@IBOutlet var mySwitch: UISwitch!
@IBOutlet var mySwitchTwo: UISwitch!
var localNotification:UILocalNotification = UILocalNotification()
var localNotificationTwo:UILocalNotification = UILocalNotification()
func datePicker() myDatePicker.datePickerMode = UIDatePickerMode.Date
func datePickerDefaultDate() myDatePicker.date = NSDate().xDays(+1)
func toggleSwitch()
if mySwitch.on
localNotification.alertAction = "Open App"
localNotification.alertBody = "Please take your medication."
localNotification.fireDate = myDatePicker.date.fireDate
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay
localNotification.timeZone = NSTimeZone.localTimeZone()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
else
localNotification.alertAction = "Open App"
localNotification.alertBody = "This notification should not appear"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999)
func toggleSwitchTwo()
if mySwitchTwo.on
localNotification.alertAction = "Open App"
localNotification.alertBody = "Please take your medication."
localNotification.fireDate = myDatePicker.date.fireDateTwo
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay
localNotification.timeZone = NSTimeZone.localTimeZone()
UIApplication.sharedApplication().scheduleLocalNotification(localNotificationTwo)
else
localNotification.alertAction = "Open App"
localNotification.alertBody = "This notification should not appear"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999)
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
@IBAction func switchPressed(sender: AnyObject)
toggleSwitch()
@IBAction func switchPressedTwo(sender: AnyObject)
toggleSwitchTwo()
这是第一个带有第一次切换时间的swift文件:
import Foundation
public 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: 8, minute: 0, second: 0, nanosecond: 0)!
我希望下次通知时间为下午 1 点。这是我为此制作的 swift 文件:
import Foundation
public extension NSDate
func xDaysTwo(x:Int) -> NSDate
return NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: x, toDate: self, options: nil)!
var dayTwo: Int return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay, fromDate: self).day
var monthTwo: Int return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth, fromDate: self).month
var yearTwo: Int return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear, fromDate: self).year
var fireDateTwo: NSDate return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 13, minute: 0, second: 0, nanosecond: 0)!
我遇到了问题,非常感谢您的帮助,在此先感谢!
【问题讨论】:
你有什么问题?实际发生了什么? 当我构建它时,它说构建成功,但是当我点击带有开关和日期选择器的按钮时,我收到一个 SIGABRT 错误@Dima 您在使用界面构建器/情节提要吗?你真的连接了 IBOutlets/IBActions 吗? 我用 ctrl + drag 把它们连接起来。在我有第二个开关之前,它工作得很好。我可以将第一个开关与 fireDateTwo 连接,然后它会触发。但是现在我有了第二个开关,我得到了那个错误。 @Dima 你不需要两个扩展名 【参考方案1】:import UIKit
class ViewController: UIViewController
@IBOutlet var myDatePicker: UIDatePicker!
@IBOutlet var mySwitch7am: UISwitch!
@IBOutlet var mySwitch8am: UISwitch!
var localNotification7am = UILocalNotification()
var localNotification8am = UILocalNotification()
var notificationsCounter = 0
func datePicker() myDatePicker.datePickerMode = UIDatePickerMode.Date
func datePickerDefaultDate() myDatePicker.date = NSDate().hour > 8 ? NSDate().xDays(+1) : NSDate()
func notificationsOptions7am()
localNotification7am.timeZone = NSTimeZone.localTimeZone()
localNotification7am.repeatInterval = .CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification7am)
localNotification7am.alertAction = "Open App"
localNotification7am.soundName = UILocalNotificationDefaultSoundName
func notificationsOptions8am()
localNotification7am.timeZone = NSTimeZone.localTimeZone()
localNotification7am.repeatInterval = .CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification8am)
localNotification7am.alertAction = "Open App"
localNotification7am.soundName = UILocalNotificationDefaultSoundName
func toggleSwitch7am()
localNotification7am.alertBody = "Here is the seven o'clock notification"
localNotification7am.fireDate = mySwitch7am.on ? myDatePicker.date.fireDateAt7am : NSDate().xDays(+36500)
func toggleSwitch8am()
localNotification8am.alertBody = "Here is the eight o'clock notification"
localNotification8am.fireDate = mySwitch8am.on ? myDatePicker.date.fireDateAt8am : NSDate().xDays(+36500)
override func viewDidLoad()
super.viewDidLoad()
datePicker()
datePickerDefaultDate()
notificationsOptions7am()
notificationsOptions8am()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
@IBAction func switchedOn7am(sender: AnyObject)
toggleSwitch7am()
@IBAction func switchedOn8am(sender: AnyObject)
toggleSwitch8am()
扩展:
import Foundation
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 fireDateAt7am: NSDate return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 7, minute: 0, second: 0, nanosecond: 0)!
var fireDateAt8am: NSDate return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 8, minute: 0, second: 0, nanosecond: 0)!
func fireDateAt(hr:Int, min:Int) -> NSDate
return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: hr, minute: min, second: 0, nanosecond: 0)!
【讨论】:
哦!然后我只需连接下一个开关并将开火日期更改为 fireDateAt8am?以上是关于在 Swift 中设置多次通知的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 2.0 中设置 UINavigationController 栏标题
Swift 在情节提要中设置 UIButton setBorderColor