使用NSUserdefaults保存datePicker值(xcode,swift2) [英] Save datePicker value using NSUserdefaults(xcode,swift2)

查看:90
本文介绍了使用NSUserdefaults保存datePicker值(xcode,swift2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何通过nsuserdeafaults或任何东西保存datepicker的值。例如,如果一个人选择下午5点,那么当他回到应用程序时,他应该在datepicker上看到下午5点。我不知道如何在下面包含nsusersdefault代码

How can we save the value of datepicker through nsuserdeafaults or anything.For example if a person chooses 5 pm then when he return to app he should see 5pm on the datepicker.I do not know how to include nsusersdefault into this below is the code

@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)
}

这篇关于使用NSUserdefaults保存datePicker值(xcode,swift2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆