简体   繁体   English

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

[英]ViewController passed as parameter is not being deinitialized (Swift)

Setup: I have a ViewController ProblemView and class A .设置:我有一个ViewController ProblemViewclass A I pass ProblemView to class A , so I can work on it.我将ProblemView传递给 class A ,所以我可以处理它。 It looks like this (simplified):它看起来像这样(简化):

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
    }
}

Problem: Whenever I restart this process within the app the memory increases every single time until I get memory error.问题:每当我在应用程序中重新启动此过程时,memory 每次都会增加,直到出现 memory 错误。

What I tried: I added deinit to both classes.我尝试了什么:我在两个类中都添加了deinit class A is always deinitialized but class ProblemView is not (this might be the problem?). class A总是被取消初始化,但class ProblemView不是(这可能是问题吗?)。 I also found out, that when I don't declare workOn globally but within the passView function, then it works just fine.我还发现,当我没有在全局范围内声明workOn而是在passView function 内,那么它工作得很好。 But I need have the variable globally, because I use it within many different functions of A .但是我需要全局变量,因为我在A的许多不同函数中使用它。 What could be a solution or workaround to this problem?什么可能是这个问题的解决方案或解决方法?

Strong references to each other.互相强引用。

Try to change class A:尝试更改 class 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的viewcontroller在show segue之后没有被取消初始化 - My viewcontroller is not being deinitialized after show segue RxSwift; ViewController 从未取消初始化 - RxSwift; ViewController never deinitialized 未使用传递到 Swift 闭包的参数 - Parameter passed into Swift closure not being used 当ActiveReferenceCount> 0时为什么要取消初始化对象 - Why object is being deinitialized when ActiveReferenceCount > 0 呈现后立即取消初始化UIViewControllerTransitioningDelegate - UIViewControllerTransitioningDelegate being deinitialized immediately after it is presented 根据传递的参数在另一个ViewController中调用函数 - Call function in another ViewController depending on parameter passed 无法在 swift 中显示第一个视图控制器将值传递给第二个视图控制器 - Unable to show first viewcontroller passed values to second viewcontroller in swift 在Swift中将传递的参数声明给var - Declare passed parameter to var in Swift Swift-检查是否传递了函数参数 - Swift - check if function parameter was passed 当泛型类型通过参数传递时,Swift无法推断泛型类型 - Swift can't infer generic type when generic type is being passed through a parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM