我使用下面的代码ViewControllerHome
:
var myValue = 0
override func viewDidLoad() {
super.viewDidLoad()
...
}
@objc func handleActionChat(sender: UITapGestureRecognizer) {
print("Val : \(myValue)"
myValue = 1
print("Val : \(myValue)"
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
var goController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewControllerIndex")
goController.roomId = self.sendRoom
self.present(goController, animated: true, completion: nil)
}
首先打开ViewControllerHome
,然后单击按钮,结果是:
0
1
如果我再次转到ViewControllerHome
并单击按钮,结果是:
1
1
为什么不清除缓存工作?
我想每次打开都会得到01
个结果
答案 0 :(得分:0)
那是因为您只设置一次var myValue = 0
。
如果您希望在出现视图控制器时将其重新设置为零,则在出现视图控制器时将myValue
重置:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
myValue = 0
}