ios - 使用 NSUserdefaults(xcode,swift2) 保存 datePicker 值

标签 ios xcode swift datepicker

我们如何通过 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/

相关文章:

ios - 你可以同时使用 Storyboard 和 Programmatic Code 吗?

iphone - 从其 subview 中删除WEPPopover

swift - 调用 costToNode : in subclassed GKGraphNode2D with GameplayKit

ios - Xcode场景编辑器-如何显示实体列表?

swift - 在 Swift 中分隔字符串

ios - 应用程序启动时的 EXC_ARM_BREAKPOINT - 仅当在设备上运行时

ios - 是否可以在UITableView中按页面滚动?

ios - 单个 ImageView 中的多个触摸事件

xcode - Xcode 单元测试中没有播放按钮

Swift:无法下标类型为 'Dictionary<String, NSObject>?' 的值