IBInspectable with Cocoa Touch Framework not working? (code attached)
Asked Answered
G

4

7

I can not seem to get the "titleText" IBInspectable attribute working. I have a simple framework view with a UILabel which I create an IBInspectable variable "titleText". I note the inspectable "layer" variables work as expected, but not my custom "titleText" which is supposed to update the main view using this framework.

Issues are:

  1. Interface Builder not updating the titleLabel? (i.e. at design time) i.e. I'm expecting that this should, just like it is working for the "layer" items.

  2. At Run Time the value of titleLabel is not picking up the value I set in IB either. Note that I get the following output when I run, i.e. my code that produces this is finding the "self.titleLabel" is actually nil??

CODE SUMMARY

enter image description here

(a) gcCustomView.xib Snapshot enter image description here

(b) gcCustomerView.swift

import UIKit

@IBDesignable 
class gcCustomView: UIView {

    @IBOutlet weak var titleLabel: UILabel!
    @IBInspectable var titleText: String = "Default" {
        didSet {
            if  self.titleLabel != nil {
                self.titleLabel.text = titleText
            } else {
                NSLog("Could not set title text as label.text was nil : was trying to set to \(titleText)")
            }
        }
    }


    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet {
            layer.cornerRadius = cornerRadius
            layer.masksToBounds = cornerRadius > 0
        }
    }
    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }
    @IBInspectable var borderColor: UIColor? {
        didSet {
            layer.borderColor = borderColor?.cgColor
        }
    }


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

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

    // Private

    func commitInit() {
        if self.subviews.count == 0 {
            print("Loading Nib")
            //let bundle = Bundle(forClass: self.dynamicType)
            let bundle = Bundle(for: type(of: self))
            let nib = UINib(nibName: "gcCustomView", bundle: bundle)
            let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
            view.frame = bounds
            view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            addSubview(view)
        }
    }

}

(c) Main.storyboard Snapshot

enter image description here

Galcha answered 12/11, 2016 at 6:4 Comment(0)
C
4

Select gcCustomView.xib File's Owner change class name to gcCustomerView.

enter image description here

Right click on the Label drag from outlet to File Owner

enter image description here

Now It should look like this if you right click Label

enter image description here

I made a demo project. It is working ok. If you still have problem let me know. I'll upload my demo project in Github.

Classroom answered 16/11, 2016 at 9:11 Comment(1)
many thanks - this answer aligns the best to where I went wrong and documented how to correct thingsGalcha
M
2

if you want to use xib make sure you have set your class name in fileowner

to do that first

enter image description here

Now add class name

enter image description here

And add IBOutlet

refer Reuse a uiview xib in storyboard

Modla answered 16/11, 2016 at 5:21 Comment(1)
I added the fileowner but this didn't help...I'll review the link you sentGalcha
J
2

This is the memory issue.Label has no memory here. Therefore it returns 0 Instead of loading the nib in custom class you should load it in ViewController. Replace your gcCustomView with the following code.

gcCustomView.swift

import UIKit

@IBDesignable
class gcCustomView: UIView {

    @IBOutlet weak var titleLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        if  self.titleLabel != nil {
            self.titleLabel.text = "Default"
        }
        else {
            NSLog("Could not set title text as label.text was nil : was trying to set to default")
        }
    }
    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet {
            layer.cornerRadius = cornerRadius
            layer.masksToBounds = cornerRadius > 0
        }
    }
    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }
    @IBInspectable var borderColor: UIColor? {
        didSet {
            layer.borderColor = borderColor?.cgColor
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
       // commitInit()
    }

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

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.loadNib()

    }
    func loadNib() {
        let obj = Bundle.main.loadNibNamed("gcCustomView", owner: nil, options: nil)?[0] as! gcCustomView
        obj.frame = (self.view.bounds)
        self.view.addSubview(obj)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Then build. It will work.

Johannessen answered 16/11, 2016 at 8:10 Comment(0)
A
0

Remove Keypath parameter in custom view and run.It's worked for me. :)

Auxin answered 21/12, 2016 at 5:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.