Comments (4)
Hi
The difference between the two protocols isn't wether you load them by XIB vs code but in how your XIB file is designed and wether your UIView subclass in your XIB is set as File's Owner or as root view.
So what I'd recommend you is to use NibOwnerLoadable
only, because it works well when integrated with another XIB or storyboard… but can also be loaded by code. If you want to load a NibOwnerLoadable
view by code simply create an instance of your view then can loadNibContent()
on that instance and you're all set.
from reusable.
Yes, I know. My code like below:
`
// CustomView has xib and some propeties outlet.
final class CustomView: UIView, NibOwnerLoadable { }
// AViewController has xib, and a CustomView, it can load it.
final class AViewController: UIViewController {
@IBOutlet weak var customView: CustomView!
}
// BViewController doesn't have xib, and a CustomView, it can load it.
final class BViewController: UIViewController {
fileprivate var customView: CustomView! = {
/*
load with code.
using nib methods it would occure an error, saying "[<NSObject 0x7f80896418b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key 'xxxxx'."
*/
}()
}
`
I try to load it but it's error. How is the NibOwnerLoadable loading by code ? Like you demo?
from reusable.
What do you mean by your comment "using nib methods it would…"? What "nib methods" did you try to load your CustomView by code?
As suggested above, you should just be able to do this to load your CustomView by code:
final class BViewController: UIViewController {
fileprivate var customView: CustomView! = {
let v = CustomView(frame: .zero) // or whatever frame you want
v.loadNibContent()
return v
}()
}
from reusable.
My NibOwnerLoadable code like below:
fileprivate var customView: CustomView! = { let nib = UINib(nibName: "CustomView", bundle: Bundle.main) let views = nib.instantiate(withOwner: nil, options: nil)// Xcode will raise an NSUnknownKeyException while you have @IBOutlet propeties let view = views.first as! CustomView retrun view }()
I didn't think of using "initWithFrame" then "loadNibContent".
Your reply is correct, Thanks.
from reusable.
Related Issues (20)
- getting error on using cellType(for indexPath:)
- Support for new API introduced on iOS 13
- MKMapView support
- Feature request - allow to use UITableViewCell with custom style
- Swift Package Manager: reusable deployment target warning HOT 3
- SPM xibs (Bundle.module) HOT 1
- Xcode 12.5 warnings HOT 2
- Swift tools version update too high, lower version build failed
- UITableViewHeaderFooterView - Warning with NibReusable
- Sure, go ahead!
- ## Summary
- Feature request - access to root xib view
- Problem while compiling with Carthage HOT 3
- NibReusable should have bundle as an optional parameter HOT 5
- Swift 5 Support? HOT 2
- Undefined symbols when compiling for unit tests HOT 1
- Xcode 11 beta 4 compiler error HOT 1
- loadNibContent always loads nib named after base class HOT 14
- is there a way to override sceneStoryboard for StoryboardSceneBased HOT 2
- Swift Package Manager HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reusable.