segued class 时 userDefualt 不保存开关
userDefualt not saving switch when class is segued
我的 swift 下面的代码都是代码,没有情节提要。尝试使用用户默认值保存开关状态。因此,当用户转至两个 ViewController,然后转回 ViewController。未保存用户开关设置。没有出现任何错误,我不知道发生了什么。
import UIKit
class ViewController: UIViewController {
var nxtBTn = UIButton()
var mySwitch = UISwitch()
let userDefaults = UserDefaults.standard
var firstTimeAppLaunch: Bool {
get {
// Will return false when the key is not set.
return userDefaults.bool(forKey: "firstTimeAppLaunch")
}
set {}
}
@objc func switchAction(_ sender: UISwitch) {
userDefaults.set(sender.isOn, forKey: "mySwitchValue")
}
@objc func press() {
let segue = twoViewController()
segue.modalPresentationStyle = .fullScreen // actually .fullScreen would be better
self.present(segue, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
[nxtBTn,mySwitch].forEach {
[=11=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=11=])
[=11=].backgroundColor = .green
}
if !firstTimeAppLaunch {
// This will only be trigger first time the application is launched.
userDefaults.set(true, forKey: "firstTimeAppLaunch")
userDefaults.set(true, forKey: "mySwitchValue")
}
NSLayoutConstraint.activate([
nxtBTn.leadingAnchor.constraint(equalTo: view.leadingAnchor),
nxtBTn.topAnchor.constraint(equalTo: view.topAnchor),
nxtBTn.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 7/7, constant: 0),
nxtBTn.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4, constant: 0),
mySwitch.leadingAnchor.constraint(equalTo: view.leadingAnchor),
mySwitch.bottomAnchor.constraint(equalTo: view.bottomAnchor),
mySwitch.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 7/7, constant: 0),
mySwitch.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4, constant: 0),
])
mySwitch.addTarget(self, action: #selector(switchAction(_:)), for: .touchDown)
nxtBTn.addTarget(self, action: #selector(press), for: .touchDown)
view.backgroundColor = .white
}
override func viewDidAppear(_ animated: Bool) {
mySwitch.isOn = userDefaults.bool(forKey: "mySwitchValue")
}
}
class twoViewController : UIViewController{
var backBtn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
backBtn.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(backBtn)
backBtn.backgroundColor = .systemGreen
NSLayoutConstraint.activate([
backBtn.leadingAnchor.constraint(equalTo: view.leadingAnchor),
backBtn.topAnchor.constraint(equalTo: view.topAnchor),
backBtn.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 7/7, constant: 0),
backBtn.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4, constant: 0),
])
backBtn.addTarget(self, action: #selector(oneVC), for: .touchDown)
}
@objc func oneVC(){
let segue = ViewController()
segue.modalPresentationStyle = .fullScreen // actually .fullScreen would be better
self.present(segue, animated: true)
}
}
你到处都在说 for: .touchDown
这是错误的。将它们全部更改为 for: .primaryActionTriggered
,情况会有所改善。
我的 swift 下面的代码都是代码,没有情节提要。尝试使用用户默认值保存开关状态。因此,当用户转至两个 ViewController,然后转回 ViewController。未保存用户开关设置。没有出现任何错误,我不知道发生了什么。
import UIKit
class ViewController: UIViewController {
var nxtBTn = UIButton()
var mySwitch = UISwitch()
let userDefaults = UserDefaults.standard
var firstTimeAppLaunch: Bool {
get {
// Will return false when the key is not set.
return userDefaults.bool(forKey: "firstTimeAppLaunch")
}
set {}
}
@objc func switchAction(_ sender: UISwitch) {
userDefaults.set(sender.isOn, forKey: "mySwitchValue")
}
@objc func press() {
let segue = twoViewController()
segue.modalPresentationStyle = .fullScreen // actually .fullScreen would be better
self.present(segue, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
[nxtBTn,mySwitch].forEach {
[=11=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=11=])
[=11=].backgroundColor = .green
}
if !firstTimeAppLaunch {
// This will only be trigger first time the application is launched.
userDefaults.set(true, forKey: "firstTimeAppLaunch")
userDefaults.set(true, forKey: "mySwitchValue")
}
NSLayoutConstraint.activate([
nxtBTn.leadingAnchor.constraint(equalTo: view.leadingAnchor),
nxtBTn.topAnchor.constraint(equalTo: view.topAnchor),
nxtBTn.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 7/7, constant: 0),
nxtBTn.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4, constant: 0),
mySwitch.leadingAnchor.constraint(equalTo: view.leadingAnchor),
mySwitch.bottomAnchor.constraint(equalTo: view.bottomAnchor),
mySwitch.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 7/7, constant: 0),
mySwitch.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4, constant: 0),
])
mySwitch.addTarget(self, action: #selector(switchAction(_:)), for: .touchDown)
nxtBTn.addTarget(self, action: #selector(press), for: .touchDown)
view.backgroundColor = .white
}
override func viewDidAppear(_ animated: Bool) {
mySwitch.isOn = userDefaults.bool(forKey: "mySwitchValue")
}
}
class twoViewController : UIViewController{
var backBtn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
backBtn.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(backBtn)
backBtn.backgroundColor = .systemGreen
NSLayoutConstraint.activate([
backBtn.leadingAnchor.constraint(equalTo: view.leadingAnchor),
backBtn.topAnchor.constraint(equalTo: view.topAnchor),
backBtn.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 7/7, constant: 0),
backBtn.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4, constant: 0),
])
backBtn.addTarget(self, action: #selector(oneVC), for: .touchDown)
}
@objc func oneVC(){
let segue = ViewController()
segue.modalPresentationStyle = .fullScreen // actually .fullScreen would be better
self.present(segue, animated: true)
}
}
你到处都在说 for: .touchDown
这是错误的。将它们全部更改为 for: .primaryActionTriggered
,情况会有所改善。