我正在制作待办事项列表应用程序。我希望能够保存我的开关选择以及开关打开的日期,以表明列表中的项目已完成。我存储了数组,但我不知道如何保存日期和切换。我现在只会显示第一个项目。
这是我的TodolistManager:
import Foundation
class ToDoListManager {
private static let userDefaults = UserDefaults.standard
private static let listKey = "ToDoListKey"
var toDoItems: [ToDoItem]
var completedSwitch: Bool
init() {
self.toDoItems = ToDoListManager.retrieve() ?? [ToDoItem]()
//* I don't know how to initialize my completedSwitch
/* self.completedSwitch =
*//
}
func getItems() -> [ToDoItem] {
return self.toDoItems
}
func numberItems() -> Int {
return self.toDoItems.count
}
func addItem(item: ToDoItem) {
self.toDoItems.append(item)
ToDoListManager.store(items: self.toDoItems)
}
func removeItem(index: Int) {
self.toDoItems.remove(at: index)
}
func getFirstItem() -> ToDoItem? {
return self.toDoItems.first
}
func getDateCompleted() -> Bool {
if completedSwitch == true{
return true;
} else {
return false;
}
}
private class func store(items:[ToDoItem]) {
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(items) {
userDefaults.set(encoded, forKey: listKey)
}
userDefaults.synchronize()
}
private class func retrieve() -> [ToDoItem]? {
if let items = userDefaults.value(forKey: listKey) as? Data {
let decoder = JSONDecoder()
if let objectsDecoded = try? decoder.decode(Array.self, from:
items) as [ToDoItem] {
return objectsDecoded
}
else {
return nil
}
}
else {
return nil
}
}
}
我的VC:
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var dateAddedLabel: UILabel!
@IBOutlet weak var completed: UISwitch!
@IBOutlet weak var dateCompletedLabel: UILabel!
@IBAction func completedSwitch(_ sender: UISwitch) {
if sender.isOn {
dateCompletedLabel.isHidden = true
}
else {
dateCompletedLabel.isHidden = false
}
}
var toDoItem: ToDoItem! {
didSet(newItem) {
self.refreshUI()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
refreshUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func refreshUI() {
if(toDoItem == nil) {
return
}
descriptionLabel?.text = toDoItem.itemDescription
completed.setOn(toDoItem.completed, animated: true)
dateAddedLabel?.text = toDoItem.getDateAdded()
dateCompletedLabel?.text = toDoItem.getDateCompleted()
}
}
这只是一个示例。
您也可以通过VC中任何地方的completedSwitch.isOn
获得其值。
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var dateAddedLabel: UILabel!
@IBOutlet weak var completedSwitch: UISwitch!
@IBAction func completedSwitch(_ sender: UISwitch) {
if sender.isOn {
///// do your stuffs if completed
}
else {
///// do your stuffs if NOT completed
}
}
var toDoItem: ToDoItem! {
didSet(newItem) {
self.refreshUI()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
refreshUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func refreshUI() {
if(toDoItem == nil) {
return
}
descriptionLabel?.text = toDoItem.itemDescription
completedSwitch?.setOn(toDoItem.completed, animated: true)
dateAddedLabel?.text = toDoItem.getDateAdded()
}
}