菜单索引路径保存 UserDefaults swift
Menu indexpath save UserDefaults swift
我想保存上次在菜单中单击的索引路径。他现在正在记录索引路径。但是,如果我选择 indexpath,他正在记录它。如果我不选择,它就不会。即使我没有从菜单中选择 select 项,我如何保留之前的记录?
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()
}
每次单击菜单时,都会删除以前的用户默认值并设置新的用户默认值。
放置此代码并每次都获取新索引,如果未选择,则您将在 Userdefaults
中获取旧值
let userDefaults = UserDefaults.standard
userDefaults.removeObject(forKey: "index")
userDefaults.set(i, forKey: "index")
编辑1
在你只定义一次常量的地方使用它
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()
}
我想保存上次在菜单中单击的索引路径。他现在正在记录索引路径。但是,如果我选择 indexpath,他正在记录它。如果我不选择,它就不会。即使我没有从菜单中选择 select 项,我如何保留之前的记录?
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()
}
每次单击菜单时,都会删除以前的用户默认值并设置新的用户默认值。 放置此代码并每次都获取新索引,如果未选择,则您将在 Userdefaults
中获取旧值 let userDefaults = UserDefaults.standard
userDefaults.removeObject(forKey: "index")
userDefaults.set(i, forKey: "index")
编辑1 在你只定义一次常量的地方使用它
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()
}