我们如何通过 nsuserdefaults 或其他方式保存日期选择器的值。例如,如果一个人选择下午 5 点,那么当他返回应用程序时,他应该在日期选择器上看到下午 5 点。我不知道如何将 nsusersdefault 包含在下面的内容中代码
@IBAction func NotificationButtonTapped(sender: AnyObject)
{
cancelLocalNotificationsWithUUID("NotificationID")
//dateformatter for alert
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh:mm a"
var strDate = dateFormatter.stringFromDate(datePicker.date)
var notifications:UILocalNotification = UILocalNotification()
notifications.fireDate = datePicker.date
notifications.timeZone = NSTimeZone.defaultTimeZone()
notifications.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
notifications.soundName = UILocalNotificationDefaultSoundName
notifications.repeatInterval = NSCalendarUnit.CalendarUnitDay
notifications.userInfo = ["UUID": "NotificationID"]
notifications.alertBody = "Quote of the day"
UIApplication.sharedApplication().scheduleLocalNotification(notifications)
}
最佳答案
您可以执行以下操作:
var datePicker = /* Get Your Date Picker from somewhere */
// Store value using User Defaults
let currentDate = datePicker.date
NSUserDefaults.standardUserDefaults().setObject(currentDate, forKey: "Current-Date")
// Retrieve Value using User Defaults
if let date = NSUserDefaults.standardUserDefaults().objectForKey("Current-Date") as? NSDate {
datePicker.setDate(date, animated: true)
}
关于ios - 使用 NSUserdefaults(xcode,swift2) 保存 datePicker 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31936095/