从 UIViewController 隐藏自定义视图 UIButton Class

Hide Custom View UIButton From UIViewController Class

实际上我有一个带有两个按钮的自定义视图,我想在运行时通过 UIViewController 隐藏它,所以我没有得到任何确切的东西来从 UIViewcontroller 隐藏那个按钮 class

这是我的 CustomView class,

 import UIKit

class BottomButtonUIView: UIView {

    @IBOutlet weak var btnNewOrder: UIButton!
    @IBOutlet weak var btnChat: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()

    }

    // MARK: init
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        if self.subviews.count == 0 {
            setup()
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    func setup() {
        if let view = Bundle.main.loadNibNamed("BottomButtonUIView", owner: self, options: nil)?.first as? BottomButtonUIView {
            view.frame = bounds
            view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

            addSubview(view)
        }
    }


    @IBAction func btnOrderNowClick(_ sender: Any) {


        let VC1 = StoryBoardModel.orderDeatalStoryBord.instantiateViewController(withIdentifier: "NewOrderViewController") as! NewOrderViewController
        VC1.isPush = false
        let navController = UINavigationController(rootViewController: VC1) // Creating a navigation controller with VC1 at the root of the navigation stack.

        let currentController = getCurrentVC.getCurrentViewController()
        currentController?.present(navController, animated:true, completion: nil)
    }
    @IBAction func btnChatNowClick(_ sender: Any) {
    }

    func getCurrentViewController() -> UIViewController? {

        if let rootController = UIApplication.shared.keyWindow?.rootViewController {
            var currentController: UIViewController! = rootController
            while( currentController.presentedViewController != nil ) {
                currentController = currentController.presentedViewController
            }
            return currentController
        }
        return nil

    }

}

我在 StoryBoard 中将其设置为 UIView,然后创建该视图的出口,

 @IBOutlet weak var viewBottmNewOrder: BottomButtonUIView!

现在我想从 UIViewcontroller class 中隐藏 btnNewOrder 但是当我使用

 viewBottmNewOrder.btnNewOrder.isHidden = true it cause null exception, Please do need full answer.

请不要那样做。当 BottomButtonUIView 从 xib 创建时, required init(coder aDecoder: NSCoder) 会调用很多次。您的自定义视图将如下所示:

[BottomButtonUIView [ BottomButtonUIView [btnNewOrder, btnChat]]].

因此,当您像这样访问 btnNewOrder 时: viewBottmNewOrder.btnNewOrder 它将为空。

我认为您应该在 `UIViewController' 的 viewDidLoad 中添加您的自定义视图。