Try to search your question here, if you can't find : Ask Any Question Now ?

Unable to access UIView subclass IBOutlets From TabBarController

HomeCategory: stackoverflowUnable to access UIView subclass IBOutlets From TabBarController
Avatarbhawya asked 1 month ago

I have a UIView Class with xib. I try to add it in another ViewControllers as a popover view. I have outlet connections. But when I run the application it crashed and stated

This class is not key value coding-compliant for the key btnAbtUs

I think the problem is should select delegate. I may using wrong way to add this xib. Any one please help me to correct. Here is my code.

//UIView Class

class MoreView: UIView {

    @IBOutlet var containerView: UIView!

    @IBOutlet weak var btnAboutUs: UIButton!


    override public func awakeFromNib() {
        super.awakeFromNib()
    }

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

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        loadViewFromNib()
    }

    func loadViewFromNib() {
        let bundle = Bundle(for: type(of: self))

        let nib = UINib(nibName: "MoreView", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

        view.frame = bounds
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        self.insertSubview(view, at: 0)

        commitInit()
    }

    private func commitInit(){

        containerView.translatesAutoresizingMaskIntoConstraints = true

        self.btnAboutUs.addTarget(self, action: #selector(self.clickAboutUs(_:)), for: .touchUpInside)
    }

    class func instanceFromNib() -> UIView {

        return UINib(nibName: "MoreView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
    }

    @objc func clickAboutUs(_ sender: Any) {
        print("tap")
    }
}



//UITabBarController

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

    let moreView = MoreView.instanceFromNib

    if let navigationController = viewController as? UINavigationController,
        navigationController.viewControllers.contains(where: { $0 is MoreViewController }) {
        moreView().frame.origin.y = 100
        self.view.addSubview(moreView())

        return false
    } else  {
        moreView().removeFromSuperview()
        return true
    }
}
1 Answers
Best Answer
Avatarnaveen answered 1 month ago
Your Answer

16 + 11 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com