Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load View/ViewController from Nib file #162

Closed
piv199 opened this issue Jul 25, 2016 · 12 comments
Closed

Load View/ViewController from Nib file #162

piv199 opened this issue Jul 25, 2016 · 12 comments
Milestone

Comments

@piv199
Copy link
Collaborator

piv199 commented Jul 25, 2016

Now it looks like the following

let filterHeaderView = UINib(nibName: FilterHeaderView.className, bundle: nil).instantiateWithOwner(self, options: nil).last as? FilterHeaderView

Not as Swifty as it could be. Should we have an extension for that?

Well I don't actually know yet how it could be more swifty. Could anybody give an example? Something like let filterHeaderView = UINib.instantiate(view: FilterHeaderView) - loads nib from nil bundle with class name name, searches for view with passed class and returns it. Or something more controllable.

@Esqarrouth
Copy link
Owner

What else needs to be controlled?

@piv199
Copy link
Collaborator Author

piv199 commented Jul 29, 2016

Just found that we already have something similar in EZSwiftExtensions.

let view: CustomView = NSBundle.loadNib("nibName") or let view =NSBundle.loadNib("nibName") as? CustomView

However it's a bit confusing as I suppose. Also it's not SwiftLint compliant.

We should control View/ViewController that will be instantiated by its type, nib should also have the same name, thus it's good practice to have the same name for View class and it's xib.

Hm... for ViewController it's not the best approach to always name xib the same as class name. So, perhaps, we should control nib name, class to look up and overloaded owner with owner.

@Esqarrouth
Copy link
Owner

Yeah, makes sense, personally I don't use storyboards/xibs so you 2 decide

@lfarah
Copy link
Collaborator

lfarah commented Jul 31, 2016

@piv199 how would you do it? I agree with you 100%

Wanna send your Swiftier version of this?

let view: CustomView = NSBundle.loadNib("nibName") or let view =NSBundle.loadNib("nibName") as? CustomView

@lfarah
Copy link
Collaborator

lfarah commented Oct 26, 2016

@piv199 any updates on this? Let me know if you need help! 😉

@vilapuigvila
Copy link
Contributor

I have this extension for that:
public class func fromNib() -> T {
return T(nibName:String(describing: T.self), bundle: nil)
}
and then you use:
let vc:SplashViewController = .fromNib()

@lfarah
Copy link
Collaborator

lfarah commented Dec 1, 2016

@piv199 what do you think?

@vilapuigvila
Copy link
Contributor

then I create a manager first:
enum ViewControllersManager {
/// Add more cases
case splashVC
case loginVC
case signupVC
}

then ..
extension ViewControllersManager: RawRepresentable {
typealias RawValue = UIViewController
var rawValue: RawValue {
switch self {
case .splashVC: let vc:SplashViewController = .fromNib()
return vc
case .loginVC: let vc:LoginViewController = .fromNib()
return vc
case .signupVC: let vc:SignupViewController = .fromNib()
}
}

use ViewControllersManager.loginVC.rawValue

@piv199
Copy link
Collaborator Author

piv199 commented Dec 18, 2016

let vc = LoginViewController.fromNib()

Hm.. makes sense... Also, have something like

let vc = LoginViewController.fromNib(named: "Login")
let vc = LoginViewController.fromNib(named: "LoginViewController", bundle: Bundle.self)

@lfarah As for me, I like this 👍

@piv199 piv199 modified the milestone: 1.9 Dec 18, 2016
@vilapuigvila
Copy link
Contributor

@piv199 @lfarah how about that?
class func fromNib<T:UIViewController>(type: T.Type) -> T {
return T(nibName:T.self.className, bundle: nil)
}

and then:
let vc = SplashViewController.fromNib(type: SplashViewController.self)

@piv199
Copy link
Collaborator Author

piv199 commented Jan 7, 2017

@vilapuigvila

let vc = SplashViewController.fromNib(type: SplashViewController.self)
why not let vc = SplashViewController.fromNib(), if we already have T in function ?

@vilapuigvila
Copy link
Contributor

I don't be able to get Type of UIVIewContreller inside function, any ideas?
Thanks

@piv199 piv199 closed this as completed Nov 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants