0

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. How can I correct it?

Here is my code.

my UIView subclass

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")
    }
}

in 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
    }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Saravanan
  • 113
  • 1
  • 11
  • That's because you have set the class name as UIView for the wired connection. – El Tomato Jun 18 '19 at 04:52
  • so how should it be.Will i need to set it as UIViewController? – Saravanan Jun 18 '19 at 04:57
  • can you ctrl click on the outlet in the xib , and check if something have more than one connection @Saravanan ? – Mohmmad S Jun 18 '19 at 05:07
  • Checked and its have only one connection. Can you please look at the code in the Should change Delegate. is there any mistake I made? @Mohammad S – Saravanan Jun 18 '19 at 05:14
  • @Saravanan Please make a connection from `UIView` itself by right click on it you might be did it from `File's Owner`. Also right click on `File's Owner` and `UIView` and check there should not be any yellow warning. – TheTiger Jun 18 '19 at 10:19
  • Thanks for your comments. I have an another issue. I was using wrong way to add the UIView now working perpectly @TheTiger – Saravanan Jun 18 '19 at 11:52

3 Answers3

1

Finally I found the issue. This is the right way to add a UIView as a SubView controller of a UIViewController.

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

    if let navigationController = viewController as? UINavigationController,
        navigationController.viewControllers.contains(where: { $0 is MoreViewController }) {
        let mySubView : MoreView
        mySubView = MoreView(frame: CGRect(x: 0, y: 0, width: 375, height: 667) )
        self.view.addSubview(mySubView)
        return false
    } else  {
        return true
    }
}
Saravanan
  • 113
  • 1
  • 11
0

You might have copied the XIB and forgot to remove/add a connection with the IBOutlet. Please check.

It indicates that an already connected Interface Builder object is removed/renamed in its owner's source (File's Owner).

Bhavik Modi
  • 1,517
  • 14
  • 29
  • I have checked and reconnect all my outlets. if I run the project with out add outlet its working.But when I add the Outlet it got crashed – Saravanan Jun 18 '19 at 04:55
  • try to check all the post from there : https://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key-v . It really seems to be a bad connection, renaming, or file-owner problem at first glance – Olympiloutre Jun 18 '19 at 05:11
0

You may forgot to remove a connection with the IBOutlet. Please check on on show connection inspector.First click on file inspector of xib then click on show connection inspector. Shown in image.

enter image description here

Remove the broken outlets.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Amit gupta
  • 553
  • 3
  • 10
  • I have checked there is no issue in the outlets. I think the problem is in Delegate method. self.view.addsubview() may cause this issue. Will you pls look at this delegate? – Saravanan Jun 18 '19 at 05:27
  • @Saravanan Remove "private" from private func commitInit() . this may be cuasing the issue – Mantu Jun 18 '19 at 05:37
  • now what you can finally do is create the button outlets again – Mantu Jun 18 '19 at 06:26