deinit没有调用子视图控制器

时间:2016-03-18 15:43:55

标签: ios swift uiviewcontroller

我有一个父viewController,它在childVC中插入带有类方法的childVC。这很有效。

class func insertIntoController(parentController: UIViewController, parentView: UIView) -> VideoDoorbellLiveView? {
    guard let liveViewController = parentController.storyboard?.instantiateViewControllerWithIdentifier(liveViewIdentifier) as? VideoDoorbellLiveView else { return nil }

    liveViewController.view.translatesAutoresizingMaskIntoConstraints = false
    parentController.addChildViewController(liveViewController)
    parentView.addSubview(liveViewController.view)

    let views = ["view": liveViewController.view]
    var constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: views)
    constraints.appendContentsOf(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-0-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: views))
    parentView.addConstraints(constraints)

    liveViewController.didMoveToParentViewController(parentController)

    return liveViewController
}

父级确保控制器在那里并设置几个String变量等。工作得非常好。

    guard let controller = VideoDoorbellLiveView.insertIntoController(self, parentView: liveView) else { return }
    liveViewController = controller // liveViewController is an optional instance variable

我的问题是当parentVC消失(deinit 被调用)时,子VC放弃而不是被调用。

任何想法为什么? childVC中没有parentVC的引用。

我甚至试图在parentVC中做到这一点,但没有保证成功:

deinit {
    if let liveViewController = liveViewController {
        liveViewController.removeFromParentViewController()
    }
}

1 个答案:

答案 0 :(得分:0)

对我来说,我有一个保留周期,需要将弱的self传递给父级视图控制器中的firebase观察器,然后在这两者上调用deinit:

options: {
    bottomTab: {
        icon: val.icon,
        text: val.text,
        textColor: getColorTheme("SECONDARY", "LIGHT"),
        selectedTextColor: getColorTheme("ORANGE", "LIGHT"),
        selectedIconColor: getColorTheme("ORANGE", "LIGHT"),
        fontFamily: FONTFAMILY.SEMIBOLD,
        fontSize: FONTSIZE.FONT_12,
        selectedFontSize: FONTSIZE.FONT_12,
        selectedBackgroundColor:'red'
    }
}