Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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 调用removeFromSuperView()后,子视图(nib)不会删除_Ios_Swift_Xcode - Fatal编程技术网

Ios 调用removeFromSuperView()后,子视图(nib)不会删除

Ios 调用removeFromSuperView()后,子视图(nib)不会删除,ios,swift,xcode,Ios,Swift,Xcode,我有一个用于隔离内容的覆盖视图,我正在ViewWillAspect()中检查身份验证,并且我的Auth方法已订阅通知。如果我在任何其他视图出现之前进行身份验证,则覆盖不会显示,但它会在第一个视图上显示,并且即使在调用removeFromSuperView()后也不会消失 这是因为您每次都试图删除一个新的ForceSignInBanner。理想情况下,您应该创建一次,并保留对所创建的ForceSignInBanner的引用(作为ProtectedViewController的可选属性) 然后删除存

我有一个用于隔离内容的覆盖视图,我正在ViewWillAspect()中检查身份验证,并且我的Auth方法已订阅通知。如果我在任何其他视图出现之前进行身份验证,则覆盖不会显示,但它会在第一个视图上显示,并且即使在调用removeFromSuperView()后也不会消失


这是因为您每次都试图删除一个新的
ForceSignInBanner
。理想情况下,您应该创建一次,并保留对所创建的
ForceSignInBanner
的引用(作为
ProtectedViewController
的可选属性)

然后删除存储在属性中的
ForceSignInBanner

    class ProtectedViewController: UIViewController, ForceSignInBannerDelegate {

        // This lazily loads the view when the property is first used and sets the delegate.
        // Ideally you wouldn't force-case the `as` but I've left it for simplicity here.

        private lazy var forceSignInBannerView: ForceSignInBanner = {
            let forceSignInBannerView  = ForceSignInBanner.instanceFromNib() as! ForceSignInBanner
            forceSignInBannerView.delegate = self
            return forceSignInBannerView
        }()

        // ... your other code ... //

        fun toggleForceSignInBannerViewVisibility(isVisible: Bool) {
            if isVisible {
                view.addSubview(forceSignInBannerView)
            } else {
                forceSignInBannerView.removeFromSuperview()
            }
        }

    }

谢谢你,我不知怎么搞明白了,我也会试试你的实现。
    class ProtectedViewController: UIViewController, ForceSignInBannerDelegate {

        // This lazily loads the view when the property is first used and sets the delegate.
        // Ideally you wouldn't force-case the `as` but I've left it for simplicity here.

        private lazy var forceSignInBannerView: ForceSignInBanner = {
            let forceSignInBannerView  = ForceSignInBanner.instanceFromNib() as! ForceSignInBanner
            forceSignInBannerView.delegate = self
            return forceSignInBannerView
        }()

        // ... your other code ... //

        fun toggleForceSignInBannerViewVisibility(isVisible: Bool) {
            if isVisible {
                view.addSubview(forceSignInBannerView)
            } else {
                forceSignInBannerView.removeFromSuperview()
            }
        }

    }