Hide Custom View UIButton From UIViewController Class

冷暖自知 提交于 2019-12-25 16:46:33

问题


Actually i have a Custom view with two button, and i want to hide it at runtime through UIViewController , So i don't get any exact thing to hide that button from UIViewcontroller class

Here is my 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

    }

}

I set it to UIView in StoryBoard, and then I create outlet of that view,

 @IBOutlet weak var viewBottmNewOrder: BottomButtonUIView!

Now i want to hide btnNewOrder from UIViewcontroller class but when i use

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

回答1:


Please don't do like that. The required init(coder aDecoder: NSCoder) will call a lot of times when the BottomButtonUIView created from xib. And your custom view will look like:

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

So when you access to btnNewOrder like that: viewBottmNewOrder.btnNewOrder it will null.

I think you should add your custom view in viewDidLoad of your `UIViewController'.



来源:https://stackoverflow.com/questions/43931617/hide-custom-view-uibutton-from-uiviewcontroller-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!