Crash on DidSet from external XIB - swift

I have an external XIB that is #IBDesignable.
Inside the view that shows this XIB, I have this outlet.
#IBOutlet weak var digitizingButton: DigitizingButton! {
didSet {
digitizingButton.buttonIsBeingTouched = {[weak self] in
// bla bla
}
}
}
crash on
digitizingButton.buttonIsBeingTouched = {[weak self] in
EXC_BAD_ACCESS (code 2)
DigitizingButton loads like this:
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)
self.loadViewFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.loadViewFromNib()
}
/** Loads instance from nib with the same name. */
func loadViewFromNib() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: .init(String(describing: type(of: self))), bundle: bundle)
let myView = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
self.view = myView
self.addSubview(myView)
}
Any ideas what is going on? Thanks in advance.

Try to change the approach of your initialization.
Add an IBOutlet from the first view of your xib file to the swift file:
#IBOutlet var containerView: UIView!
Then edit your loadViewFromBib method as follow:
func loadViewFromNib() {
Bundle.main.loadNibNamed("DigitizingButton", owner: self, options: nil)
addSubview(containerView)
containerView.frame = self.bounds
containerView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
// Other initialization here
}

Related

Call to IBOutlet in custom UIView from another class

I am looking for a way to refer to the IBOutlet variable in a custom class UIView, from another class. I found layoutSubviews, but each change only works on the first call, and not on each subsequent call. Thanks for help!
ViewController class:
var SB = StatusBar()
SB.update(1)
SB.update(2)
SB.update(3)
StatusBar class:
class StatusBar: UIView {
#IBOutlet var view: UIView!
#IBOutlet weak var label: UILabel!
var ActualStatus: Int!
required init?(coder: NSCoder) {
super.init(coder: coder)
xibSetup()
}
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
func update(status) {
ActualStatus = status
self.layoutSubviews()
}
override func layoutSubviews() {
super.layoutSubviews()
label.text = ActualStatus
}
func xibSetup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth, UIView.AutoresizingMask.flexibleHeight]
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = Bundle(for: type(of:self))
let nib = UINib(nibName: "StatusBar", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
}
Result: label.text is 1, change only works on the first call

Custom Class UIView - not changed

Why in the code below, text changes as value, but there is no change in UIVIew. How do I refresh the data in a custom class? The value changes but does not refresh in the view. I have no ideas anymore.
ViewController.swift:
let SB = StatusBar()
SB.update()
StatusBar.swift:
class StatusBar: UIView {
#IBOutlet var view: UIView!
#IBOutlet weak var Status: UITextField!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
func update() {
Status.text = "ok"
print(Status.text)
print("status updated.")
}
func xibSetup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth, UIView.AutoresizingMask.flexibleHeight]
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = Bundle(for: type(of:self))
let nib = UINib(nibName: "StatusBar", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
}
You are not calling update(). In xibSetup(), add update()

Could not cast value of type UIView to [CustomView] using Xib

I've seen a number of similar questions, but many aren't up-to-date, and none have fixed my issue.
I have a custom Xib and class, CardView. When I try to instantiate it in code, I get Could not cast value of type 'UIView' to 'CardView'.
Class is as follows:
class CardView: NibView {
#IBOutlet var contentView: UIView!
#IBOutlet weak var wordLabel: UILabel!
#IBOutlet weak var flipButton: UIButton!
#IBOutlet weak var playButton: UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
Bundle.main.loadNibNamed("CardView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
and NibView is a custom superclass and looks like:
class NibView: UIView {
var view: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
// Setup view from .xib file
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Setup view from .xib file
xibSetup()
}
}
private extension NibView {
func xibSetup() {
backgroundColor = UIColor.white
view = loadNib()
// use bounds not frame or it'll be offset
view.frame = bounds
// Adding custom subview on top of our view
addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[childView]|",
options: [],
metrics: nil,
views: ["childView": view]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[childView]|",
options: [],
metrics: nil,
views: ["childView": view]))
}
}
extension UIView {
/** Loads instance from nib with the same name. */
func loadNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nibName = type(of: self).description().components(separatedBy: ".").last!
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as! UIView
}
}
Here is a screenshot of my file inspector showing that I set the File's Owner custom class to CardView, which seems to be a stumbling block for many
Finally, I try to instantiate the CardView as follows: CardView().loadNib() as! CardView
In case someone still having this problem, I have the same problem and I think we follow same tutorial.
You have called loadNib() in your xibSetup(). I think you don't have to call it again.
So instead of using
let myCardView = CardView().loadNib() as! CardView
I just use
let myCardView = CardView()
In your UIViewController, when you create an instance of your CardView, instead of casting it like CardView().loadNib() as! CardView, you call the function object_setClass(_, _) to avoid that error (from Xcode 9).
So it should be like:
let myCardView = CardView().loadNib
object_setClass(myCardView, CardView.self)
view.addSubview(myCardView)
Old question, but in case anyone's here:
My problem was that the view subclass was fileprivate. IB doesn't like that apparently.
For me loading xib programmatically in this way doesn’t work. I had to remove File owner’s class name and set that to the class name of first view. And then I had to set outlets to the components which I’m going to use.
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override init(frame: CGRect) {
super.init(frame: frame)
}
Added to custom view class after outlets. For you it's CardView. Then I loaded that view class like
let viewCard = Bundle.main.loadNibNamed("CardView", owner: self, options: nil)?.first as! CardView
view.addSubview(viewCard)

Unable to connect IBOutlet to XIB File

I am trying to implement Koloda card swiping style. I have created a .xib file structured as Image 1 & 2.
As you can see in Image 1, I have set the File's Owner as the XIB Files custom class.
In image 2, I have left the class blank (not sure if this is correct)
My NIB class is per the below code.
import UIKit
import Koloda
class CardView: KolodaView {
var view: UIView!
var nibName: String = "CardView"
var uid: String!
#IBOutlet weak var profileImage: UIImageView!
#IBOutlet weak var nameLabel: UILabel!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUp()
}
override init(frame: CGRect) {
super.init(frame: frame)
setUp()
}
func setUp() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.isUserInteractionEnabled = true
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = Bundle(for: CardView.self)
let nib = UINib(nibName: String(describing: CardView.self), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
}
In my controller, I have the view for index as per the below;
func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView {
_ = userResults[Int(index)]
let bundle = Bundle(for: CardView.self)
let nib = UINib(nibName: String(describing: CardView.self), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
I am unable to insert my IBOutlets and reference them in my UIViewController. As I will be getting geoFire & FirDatabase info and populating the views. I understand this has something to do with the File's Owner. I'm just unsure on how to set the IBOutlets to the subview instead of the custom class.
Any help appreciated.
Finally after hours of tinkering around.
Class is now;
import UIKit
class CardView: UIView {
var uid: String!
#IBOutlet weak var profileImage: UIImageView!
#IBOutlet weak var nameLabel: UILabel!
}
Removed file owner
CardView in xib is now set to CardView
viewForCardAt is now;
func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView {
let bundle = Bundle(for: CardView.self)
let nib = UINib(nibName: String(describing: CardView.self), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! CardView
let users = userResults[Int(index)]
view.profileImage.sd_setImage(with: URL(string: users.userProfileURL))
view.nameLabel.text = users.userName
view.uid = users.uid
return view
}
I now have a working "Tinder Card with geoLocating" :)
Try Setting the CardView class to custom class CardView instead of UIView

Can I Instantiate a Swift Custom UIView from Storyboard Without It Nesting Inside Itself?

Every where I look for how to make a custom UI View with a nib I see the following code to use
class CustomView: UIView {
var contentView: UIView!;
#IBOutlet weak var sampleLabel: UILabel!
init() {
super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
if self.subviews.count == 0 {
nibSetup()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
if self.subviews.count == 0 {
nibSetup()
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
if self.subviews.count == 0 {
nibSetup()
}
}
fileprivate func nibSetup() {
contentView = loadViewFromNib()
contentView.frame = bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
contentView.translatesAutoresizingMaskIntoConstraints = true
addSubview(contentView)
}
fileprivate func loadViewFromNib() -> TimerView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let nibView = nib.instantiate(withOwner: self, options: nil).first as! UIView
return nibView
}
}
But this basically loads a new instance of the custom view (Loaded from the nib) into a contentView on the original instance. This works until you want to call methods on your view from the view controller it is in. When you want to call a method on the instance from the view controller you are calling it on the original instance not the instance that is in cotnentView so the result is nothing happens.
As a work around I have declared the contentView to be a CustomView instead of a UIView and then my public methods call methods on the content view
ie
class CustomView: UIView {
var contentView: CustomView!;
#IBOutlet weak var sampleLabel: UILabel!
init() {
super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
if self.subviews.count == 0 {
nibSetup()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
if self.subviews.count == 0 {
nibSetup()
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
if self.subviews.count == 0 {
nibSetup()
}
}
fileprivate func nibSetup() {
contentView = loadViewFromNib()
contentView.frame = bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
contentView.translatesAutoresizingMaskIntoConstraints = true
addSubview(contentView)
}
fileprivate func loadViewFromNib() -> TimerView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let nibView = nib.instantiate(withOwner: self, options: nil).first as! CustomView
return nibView
}
func setLabelText(blah: String) {
sampleLabel.text = blah
}
public func setLabelTextFromParent(words: String) {
contentView.setLabelText(blah: words)
}
}
This seems really hacky though. There has to be a much better way of doing this!
Can someone explain to me how to do this so that I only have one instance of my custom view rather than one nested in another when instantiating it from IB. Thanks.
You can define an extension on UIView like
extension UIView {
class func fromNib<T : UIView>() -> T {
return Bundle.main.loadNibNamed(String(describing: T.self), owner: nil, options: nil)![0] as! T
}
}
I don't see any hack here.
Call it like
let myCustomView: MyCustomView = UIView.fromNib()