0

设置: 我有一个ViewController ProblemViewclass 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. 什么可能是这个问题的解决方案或解决方法?

4

1 回答 1

1

互相强引用。

尝试更改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
}
于 2020-12-04T00:15:01.170 回答