Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将UIViewController添加为子视图后,对其进行Denit_Ios_Swift_Uiviewcontroller_Deinit - Fatal编程技术网

Ios 将UIViewController添加为子视图后,对其进行Denit

Ios 将UIViewController添加为子视图后,对其进行Denit,ios,swift,uiviewcontroller,deinit,Ios,Swift,Uiviewcontroller,Deinit,我需要添加一个viewcontroller作为mt current view的子视图,但在我不再需要它之后无法进行Denit let viewController = CountrySelectViewController() viewController.view.frame = self.view.bounds viewController.view.alpha=0 self.view.addSubview(viewContr

我需要添加一个viewcontroller作为mt current view的子视图,但在我不再需要它之后无法进行Denit

       let viewController = CountrySelectViewController()
        viewController.view.frame = self.view.bounds

        viewController.view.alpha=0

        self.view.addSubview(viewController.view)

        UIView.animate(withDuration: 0.25, delay: 0.0, options: [], animations: {
            viewController.view.alpha=1

        }, completion: { (finished: Bool) in
        })

        viewController.completionHandlerClose = {

            UIView.animate(withDuration: 0.25, delay: 0.0, options: [], animations: {
                viewController.view.alpha=0

            }, completion: { (finished: Bool) in

                viewController.view.removeFromSuperview()
                viewController.view = nil
            })

        }

有一个明显的强参考循环,必须使用
弱参考来打破:

viewController.completionHandlerClose = { [weak viewController] in
    guard let controller = viewController else { return }
    UIView.animate(
        withDuration: 0.25,
        delay: 0.0,
        options: [],
        animations: {
           controller.view.alpha = 0
        },
        completion: { _ in
            controller.view.removeFromSuperview()
            controller.view = nil
        }
    )
}

请参见

将其设为可空,然后如果您想将其值设为零,那么如何将其设为空?yourController=nil不能将'nil'指定给'CountrySelectViewController'类型,请将您的控制器描述为var yourController:CountryController?这样,它在作为子视图添加后得到Denit,而不是在我关闭它时…@F79似乎你不知道如何使用控制器。如果你只使用它的视图,为什么还要创建一个控制器?它不仅仅是一个视图,它有方法和其他东西,这只是因为在iOS13模式视图中,它们不是全屏的,它们看起来像卡片,我需要它全屏显示…@F79,可以使用控制器的显示样式进行更改。实际上,您没有将控制器添加到层次结构中,这是一件危险的事情。@F79您错了。仅更改了默认值。您始终可以将其设置为
.fullScreen
.overFullScreen
。默认值为
.automatic
,在iOS 13上等于
.pageSheet