设置:我有一个ViewController ProblemView
和class A
。我把ProblemView
传递给A
类,这样我就可以处理它了
class ProblemView: UIViewController{
var instanceOfA = A()
instanceOfA.passView(passedVC: self)
}
class A{
var workOn = ProblemView()
func passView(passedVC: ProblemView){
workOn = passedVC
// I noticed, if I declare a varible locally like var workOn2 = passedVC, my problem is solved -
// but I need the variable globally, because I don't want to pass it around within this class
}
func doSth(){
// here I interact with variables of the passed ViewController
}
}
问题:每当我在应用程序中重新启动此进程时,内存每次都会增加,直到出现内存错误。
我尝试的内容:我将deinit
添加到两个类中。class A
总是被取消初始化,但class ProblemView
不是(这可能是问题所在?(。我还发现,当我没有全局声明workOn
,而是在passView
函数中声明时,它就可以正常工作了。但我需要全局使用这个变量,因为我在A
的许多不同函数中使用它。这个问题的解决方案或解决方法是什么?
彼此之间的强引用。
尝试更改A类:
weak var workOn: ProblemView?
func passView(passedVC: ProblemView){
workOn = passedVC
// I noticed, if I declare a varible locally like var workOn2 = passedVC, my problem is solved -
// but I need the variable globally, because I don't want to pass it around within this class
}
func doSth(){
// here I interact with variables of the passed ViewController
}