作为参数传递的 ViewController 未被取消初始化 (Swift)
ViewController passed as parameter is not being deinitialized (Swift)
设置:
我有一个 ViewController ProblemView
和 class A
。我将 ProblemView
传递给 class 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
}
}
问题: 每当我在应用程序中重新启动此进程时,每次内存都会增加,直到出现内存错误。
我尝试了什么: 我在两个 class 中添加了 deinit
。 class A
总是取消初始化,但 class ProblemView
不是 (这可能是问题所在?)。
我还发现,当我没有全局声明 workOn
而是在 passView
函数中声明时,它就可以正常工作。但是我需要全局变量,因为我在 A
的许多不同函数中使用它。这个问题的解决方案或解决方法是什么?
互相强烈引用。
尝试改class答:
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
}
设置:
我有一个 ViewController ProblemView
和 class A
。我将 ProblemView
传递给 class 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
}
}
问题: 每当我在应用程序中重新启动此进程时,每次内存都会增加,直到出现内存错误。
我尝试了什么: 我在两个 class 中添加了 deinit
。 class A
总是取消初始化,但 class ProblemView
不是 (这可能是问题所在?)。
我还发现,当我没有全局声明 workOn
而是在 passView
函数中声明时,它就可以正常工作。但是我需要全局变量,因为我在 A
的许多不同函数中使用它。这个问题的解决方案或解决方法是什么?
互相强烈引用。
尝试改class答:
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
}