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

I can't seem to get the "titleText" IBInspectable attribute to work. I have a simple wireframe view with UILabel, which I create the IBInspectable variable "titleText". I note that the "layer" checked variables work as expected, but not my custom "titleText", which should update the main view using this structure.

Problems:

  • Builder interface not updating titleLabel? (that is, during development), that is, I expect this to be the same as for the "layer" elements.

  • At run time, the titleLabel value does not match the value set to IB. Note that I get the following output when I run, that is, my code that produces this finds "self.titleLabel" is actually nil ??

SUMMARY OF CODE

enter image description here

(a) Snapshot of gcCustomView.xib 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) Snapshot of Main.storyboard

enter image description here

+6
source share
4 answers

Select gcCustomView.xib File Owner to change the class name to gcCustomerView .

enter image description here

Right click on Label drag from outlet to File Owner

enter image description here

It should now look like this if you right-click the Label

enter image description here

I did a demo project. It is working fine. If you still have problems let me know. I will upload my demo project to Github.

+4
source

if you want to use xib make sure you specify the name of your class in the file

to make it first

enter image description here

Now add class name

enter image description here

And add IBOutlet

refer Reuse uiview xib in storyboard

+2
source

This is a memory problem. Label has no memory. Therefore, it returns 0 Instead of loading the nib into a custom class, you should load it into the ViewController. Replace 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. He will work.

+2
source

Remove the Keypath parameter in the user view and run. He worked for me. :)

0
source

Source: https://habr.com/ru/post/1012258/


All Articles