Swift呈现给另一个页面无法清除缓存

时间:2018-08-18 11:08:18

标签: swift uiviewcontroller uistoryboard

我使用下面的代码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个结果

1 个答案:

答案 0 :(得分:0)

那是因为您只设置一次var myValue = 0

如果您希望在出现视图控制器时将其重新设置为零,则在出现视图控制器时将myValue重置:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    myValue = 0
}