作为参数传递的ViewController没有被取消初始化(Swift)



设置:我有一个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的许多不同函数中使用它。这个问题的解决方案或解决方法是什么?

彼此之间的强引用。

尝试更改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
}

相关内容

  • 没有找到相关文章

最新更新