我有一个奇怪的问题
index.swift
override func viewDidLoad() {
super.viewDidLoad()
// ...
goToPage.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleGoToPage(sender:))))
}
@objc func handleGoToPage(sender: UITapGestureRecognizer) {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let pageController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "page")
self.present(pageController, animated: true, completion: nil)
}
page.swift
override func viewDidLoad() {
super.viewDidLoad()
// ...
pritData.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handlePritData(sender:))))
}
@objc func handlePritData(sender: UITapGestureRecognizer) {
print("DATA")
}
我打开应用程序
我单击goToPage
按钮并转到page.swift
我单击printData
并打印DATA
直到这一刻,一切正常
我回到index.swift
我再次单击goToPage
按钮并转到page.swift
我单击printData
并打印DATA
DATA
为什么要打印两次DATA
?
如果我退回index.swift
并再次单击并转到page.swift
,则我在page.swift
中的功能会运行多个并打印DATA
个
例如,如果我20次去page.swift
并单击printData
,则为我打印20次DATA
为什么不为每个打开的page.swift
清除缓存或清除函数缓存?