我想保存菜单中上次单击的索引路径。他现在正在记录indexpath。但是,如果我选择indexpath,他会记录它。如果我不选择,那就不会。即使我没有从菜单中选择项目,如何保存上一个记录?
let save11 = UserDefaults.standard.integer(forKey: "indexPath")
let yenindex = save11
self.menuView.setSelected(index: yenindex)
let userDefaults = UserDefaults.standard
userDefaults.removeObject(forKey: "indexPath")
menuView.didSelectItemAtIndexHandler = {(indexPath: Int) -> Void in
var placesdeger: String = ""
UserDefaults.standard.setValue(indexPath, forKey: "indexPath")
if indexPath == 0 {
let userDefaults = UserDefaults.standard
userDefaults.removeObject(forKey: "indexPath")
UserDefaults.standard.setValue(indexPath, forKey: "indexPath")
placesdeger = "Yatak Odası"
}
if indexPath == 1 {
let userDefaults = UserDefaults.standard
userDefaults.removeObject(forKey: "indexPath")
UserDefaults.standard.setValue(indexPath, forKey: "indexPath")
placesdeger = "Oturma Odası"
self.refreshData()
}
答案 0 :(得分:0)
每次单击菜单时,都将删除以前的Userdefault值,并每次都设置新的userdefault值。 每次都放置此代码并获取新索引,如果未选中,则旧值将出现在Userdefaults中
let userDefaults = UserDefaults.standard
userDefaults.removeObject(forKey: "index")
userDefaults.set(i, forKey: "index")
EDIT1 仅在一次定义恒定的地方使用此方法
let userDefaults = UserDefaults.standard
如果您正在使用或我不知道的任何其他方法,但是如果您获得正确的索引路径,则此代码将起作用
menuView.didSelectItemAtIndexHandler = {(indexPath: Int) -> Void in
var placesdeger: String = ""
userDefaults.removeObject(forKey: "indexPath")
if indexPath == 0 {
let userDefaults = UserDefaults.standard
UserDefaults.standard.setValue(indexPath, forKey: "indexPath")
placesdeger = "Yatak Odası"
}
if indexPath == 1 {
UserDefaults.standard.setValue(indexPath, forKey: "indexPath")
placesdeger = "Oturma Odası"
self.refreshData()
}