在swift中从单独的nib文件加载UIViewController?
我有一个ViewController与单独的nib文件。 而我的初始根视图控制器是在storyBoard中设置的。 现在的问题是,当我推到这个控制器查看的hireachy方法不被称为(ViewDidLoad,ViewWillApper等)..
代码 (视图已加载但方法未调用)
var viewController = UIViewController(nibName: "OfferDetailViewController", bundle: nil) as OfferDetailViewController self.navigationController?.pushViewController(viewController, animated: true);
同样的事情,如果我与故事板的工作正常。
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) var viewController = mainStoryboard.instantiateViewControllerWithIdentifier("offer") as OfferDetailViewController self.navigationController?.pushViewController(viewController, animated: true);
问题 :故事板视图层次结构方法正在调用,但不是与单独的nib文件?
var viewController = OfferDetailViewController(nibName: "OfferDetailViewController", bundle: nil)
这是一个很好的通用方法…
extension UIViewController { class func loadFromNib<T: UIViewController>() -> T { return T(nibName: String(describing: self), bundle: nil) } } let vc : OfferDetailViewController = OfferDetailViewController.loadFromNib()