0

When I try to use this custom control within my main view controller, by dragging a UIView onto screen in IB and setting it to "CustomerControlView", it doesn't actually show it.

Question - What is wrong with the code I have here?

Background - So wanting to basically: a) design a customer control in IB b) so I'm assuming I create the NIB file and then create a UIView file, so this is what I've done

Screenshot of NIB & swift file

enter image description here Code

import UIKit

class CustomControlView: UIView {

//    @IBOutlet var icon: UIImageView!
//    @IBOutlet weak var view: UIView!

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var text1: UITextField!
    @IBOutlet weak var text2: UITextField!

    override init(frame: CGRect) {
        print("override init(frame: CGRect) ")
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        print("required init?(coder aDecoder: NSCoder)")
        super.init(coder: aDecoder)

//        let arr = NSBundle.mainBundle().loadNibNamed("CustomControlView", owner: nil, options: nil)
//        let v = arr[0] as! UIView
//        self.view.addSubview(v)
    }


}

Snapshot showing how I have included the custom view into my main viewController view:

enter image description here

Greg
  • 34,042
  • 79
  • 253
  • 454

1 Answers1

4

Just add @IBDesignable above the class line to tell Xcode to compile it before showing it in storyboard. There is a great discussion on this subject here: http://nshipster.com/ibinspectable-ibdesignable/

Also, you need to make sure that the NIB is being loaded:

override init(frame: CGRect) {
    print("override init(frame: CGRect) ")
    super.init(frame: frame)
    xibSetup()
}

required init?(coder aDecoder: NSCoder) {
    print("required init?(coder aDecoder: NSCoder)")
    super.init(coder: aDecoder)

    xibSetup()

}


func xibSetup() {
    view = loadViewFromNib()
    view.frame = bounds
    view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    addSubview(view)
}

func loadViewFromNib() -> UIView {
    let bundle = NSBundle(forClass: self.dynamicType)
    let nib = UINib(nibName: "CustomNumberPad", bundle: bundle)
    let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
    return view
}
Mika
  • 5,807
  • 6
  • 38
  • 83
  • thanks - can't test this at the moment, however I wanted to point out that it doesn't show the control when I run the project itself (i.e. not just a "display in IB" question). Does this make sense? – Greg Sep 30 '15 at 23:08
  • @Milka - this is giving an error EXC_BAD_ACCESS error, with the code repeatedly calling the "required init?(coder aDecoder: NSCoder)" method, as I'm seeing the print outputs in the console. Any idea how to correct this? – Greg Oct 01 '15 at 00:19
  • I found here "http://blog.boxuanzhang.me/custom-reusable-uiview-with-xib/" there is a work around by putting in a "if self.subview.count == 0 {" check in the xibSetup makes it work. Wondering if there is a better way? – Greg Oct 01 '15 at 00:39
  • actually just posted a separate question re is there a better way here: http://stackoverflow.com/questions/32877816/loading-custom-uiview-from-nib-requires-a-self-subview-count-check-to-avoid-ci – Greg Oct 01 '15 at 00:47
  • Sorry feel asleep last night... I have the exact same code for many projects and controls and never had this loop problem... Let's try to fix it: (1) Did you add the `@IBDesignable class CustomControlView`? (2) how do you use the control? – Mika Oct 01 '15 at 15:30
  • oops, missed your reply. Actually I decided it was just too frustrating (noting I'm just starting up in IOS) trying to fix all the issues associated with trying to implement re-usable components. I've just merged things back into the one story board and view controller to let me keep going. Not sure if I should accept your answer therefore? I did not others having the issue, e.g. http://blog.boxuanzhang.me/custom-reusable-uiview-with-xib/ – Greg Oct 19 '15 at 12:12