Created
March 3, 2018 05:32
-
-
Save vdeep/57823610b31c3854383aeed5bff904d1 to your computer and use it in GitHub Desktop.
UIViewController extension to instantiate view controller from storyboard or xib file of the same name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIViewController { | |
static func initWithStoryboard(_ storyboard: UIStoryboard? = nil, | |
_ identifier: String? = nil) -> UIViewController? { | |
var _storyboard = storyboard | |
if storyboard == nil { | |
_storyboard = UIStoryboard(name: String(describing: self), bundle: nil) | |
} | |
guard _storyboard != nil else { | |
return nil | |
} | |
if let identifier = identifier { | |
return _storyboard?.instantiateViewController(withIdentifier: identifier) | |
} else { | |
return _storyboard?.instantiateInitialViewController() | |
} | |
} | |
static func initWithNib() -> Self { | |
return self.init(nibName: String(describing: self), bundle: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment