Loading a NIB/XIB file with IBOutlets - ios

I want to load a UIView from a XIB file, and I was able to do so successful via:
let nib = UINib(nibName: "CardView", bundle: nil).instantiateWithOwner(nil, options: nil).first as! UIView
Where CardView is the name of my XIB file. Now when I try to add an IBOutlet to the XIB file:
I get this error:
'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key phraseLabel.
I have checked that there aren't any orphan IBOutlets in the XIB file. So thinking that it was because I didn't cast the nib to a CardView class I tried this:
let nib = UINib(nibName: "CardView", bundle: nil).instantiateWithOwner(nil, options: nil).first as! CardView
But I end up with this error:
Could not cast value of type 'UIView' (0x10c058df8) to 'LazyFlashCards.CardView' (0x10934bb00).
So I'm guessing that I'm instantiating the XIB file wrongly but I don't know the "correct" way to do it

If you're having issues with this in tests you need to set the owner to your test target.
bundle.loadNibNamed("myNib", owner: testSubject) // owner != nil

I just wrote example to load uiview from XIB by swift, you can read, hope this help you!
https://github.com/lengocgiang/loadViewFromXib

Related

Reusable nib for multiple classes

I have a case in which I would like to use one NIB (for UITableViewCell to be precise) for multiple classes. I have tried to define outlets as 'File owner' as then instantiate the cell, but I always receive crash, where xCode telling me that class is not key/value compliant for some outlet.
So my question is: is it possible to use one NIB, define outlets and then instantiate nib & class in a way, that Outlets are linked to the class?
The code I have tried to use:
let cell = UINib(nibName: "SampleTableViewCell", bundle: nil).instantiate(withOwner: DummyTableViewCell.self, options: nil).first as? SampleTableViewCell
The SampleTableViewCell nib has UITableViewCell defined for cell class (default) and FileOwner as NSObjet (default again). All IBOutlet are linked as FileOwner.

xib with UICollectionView - not key value coding compliant

I have a Custom UIView with an XIB. This custom UIView has a UICollectionView which is connected to an IBOutlet. In the view setup, the UICollectionView is initialised properly and is not nil.
However in the cellForItemAtIndexPath method, I get this error:-
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key selectorCollectionView.'
If I remove the datasource and delegate, I do not get any error. If I add them Iget an error at this line:-
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "selectorCell", for: indexPath) as! SelectorCell
Please help!
Edit: I have attached screenshots of my setup
I have uploaded the project here too http://www.fast-files.com/getfile.aspx?file=148623
Maybe a sync issue. Happens sometimes:
Try cut outlets loose and reconnect them.
Make sure Collection Reusable View identifier is defined in xib file:
Make sure collection-view cell's custom class is define in xib file:
EDIT:
I dug into your project and here are my findings
UICollectionView should be init' in awakeFromNib method (override it):
override func awakeFromNib() {
super.awakeFromNib()
let cellNib = UINib(nibName: String(describing: "SelectorCell"), bundle: nil)
selectorCollectionView.register(cellNib, forCellWithReuseIdentifier: "selectorCell")
selectorCollectionView.dataSource = self
selectorCollectionView.delegate = self
}
loadViewFromNib should be looking like this (Remove collection view init'):
func loadViewFromNib() -> UIView {
let nib = UINib(nibName: "SelectorView", bundle: nil)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
Subclass SelectorTableViewCell likewise.
class SelectorTableViewCell: UITableViewCell {
#IBOutlet weak var selectorView: SelectorView!
}
In Main.storyboard - custom class UITableViewCell to SelectorTableViewCell and connect SelectorView inside contentView to 'SelectorTableViewCell''s outlet.
That's it i think. Here is the project:
You can try the following:
Click on File Owner at the left panel of YourCell.xib. Then go to Attributes inspector and check whether there is any class specified in Class field. If not - no help from me here. If yes - remove it and tap Enter.
In YourCell.xib sequentially click on all subviews and delete all the outlets in Connections Inspector. No need to delete them in YourCell.swift.
Once your cell's File Owner and outlets are removed, we're ready to reconnect the outlets. Just drag every one as usually to an existing corresponding IBOutlet in YourCell.swift.
As a result, when you click on YourCell at the left panel of YourCell.xib and go to Connections Inspector, you'll see there are all the outlets connected. Like so:
And if you check connections for a particular view in YourCell, you should see that it's outlet is connected to YourCell (StaticSlot in my case).
This helped me and hope you'll get the same.
In our case, the problem was making the wrong connection in Storyboard. Click on the custom class in Storyboard -- not the File Owner -- then connect the IB outlet to the variable in the custom class.
if try to register you custom class and cell ID
self.collectionView.register(nib, forCellWithReuseIdentifier: "Cell")
self.collectionView.register(CustomClassCell.self, forCellWithReuseIdentifier: "Cell")

Opening a UIView from another UIView

I have a UIViewController with a button that opens a Modal/Popup (XIB)
I use this code for that:
let aView: myView1 = UINib(nibname: "AView", bundle : nil).instantitate(withOwner: self, options: nil)[0] as! myView1
aView.frame = self.view.frame
self.view.addSubView(aView)
The Modal contains a button that should open a Form (another XIB)
I use this code for that:
let bView: myView2 = UINib(nibname: "BView", bundle : nil).instantitate(withOwner: self, options: nil)[0] as! myView2
bView.frame = self.view.frame
self.view.addSubView(bView)
But I get a compile time error, saying:
Value of type 'AView' has no member 'view'
When I changed the code from:
self.view.addSubview(bView)
to:
self.addSubview(bView)
I get a runtime error when using above line in UIView:
this class is not key value coding-compliant for the key view.
If I relocate the above line to UIViewController, I get a different error:
Can't add self as subview
Is there some other way that works to open a UIView from another UIView, without going back or making changes to the UIViewController?
First i suggest change class name from myView1 to MyView1, same to myView2. Then set your nib files file's owner from interface builder to MyView1 and MyView1.
Change this withOwner: self to nil in below call:
let aView:myView1 = UINib(nibname: "AView", bundle : nil).instantitate(withOwner: self, options: nil)[0] as! myView1
OR
You might need to remove file's owner from interface builder.

Swift3 Unable to link referencing outlet

I'm defining a Custom UIView based on a XIB. Therefor I have copied CustomXIBSwift-master from Github. That works very well. I adapted it briefly for my purposes.
The label (lblTitle) is linked as a referencing outlet to the File's Owner
Now I also want to make a referencing outlet for the second sublabel to File's Owner. However I'm not able to linke this using the dot-line method nor by including the IBOutlet manually.
Based on the CustomXIBSwift-master the XIB is of type UIView. When I specify a custom class (instead of UIView) it crashes.
Part of the SWIFT code:
func loadViewFromNib() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "SlidingAlertView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(view);
}
How can I make a referencing outlet from this View to the Swift file?
I was not able to make a connection between the UILabels and the File's Owner. It appeared that also for the File's Owner thee Custom Class can be specified.

Make view controller in run time with xibs

I want to make view controller at run time with the nib name like this,
But this is being crashed.
Error
[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imageViewApp.
let vc = UIViewController(nibName: nibName, bundle: nil)
When I try with the static values like this
let vc = MyViewController(nibName: "MyViewController", bundle: nil), it is working.
This is the IBOutlet in MyViewController
I googled it but haven't found enough solution please let me know the solution.
You're initializing a UIViewController and UIViewController has no imageViewApp property. You need to initialize your own subclass where you define that property, as you do in your second example.
Try something like this:
let vc = MyViewController(nibName: nibName, bundle: nil)
If your nib file has the same name as your class, you don't have to specify it:
let vc = MyViewController(nibName: nil, bundle: nil)
Remove imageViewApp property IBOutlet from that view controller and give that ImageView a tag and access it with the tag.

Resources