GithubHelp home page GithubHelp logo

Comments (4)

AliSoftware avatar AliSoftware commented on July 20, 2024

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.

codwam avatar codwam commented on July 20, 2024

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.

AliSoftware avatar AliSoftware commented on July 20, 2024

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.

codwam avatar codwam commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.