使用泛型使用Nib加载ViewController

Dee*_*are 3 generics uiviewcontroller nib ios swift

目前以相同的nibname加载viewcontroller我使用代码如下

let recommendationVC : RecommendationVC = RecommendationVC(nibName: "RecommendationVC", bundle: nil)
Run Code Online (Sandbox Code Playgroud)

我感觉不需要指定nibname,因为它与控制器名称相同。所以我决定使用泛型,并使用泛型推断类型和笔尖名称

protocol NibIdentifiable {
    static var nibNameIdentifier: String { get }
}

// MARK: - Indentifies each storyboard from its classname.
extension NibIdentifiable where Self: UIViewController {
    static var nibNameIdentifier: String {
        return String(describing: self)
    }
}
extension UIViewController :NibIdentifiable
{

}

extension UIViewController {
    func instantiate<Controller: UIViewController>(_: Controller.Type) -> Controller where Controller: NibIdentifiable {

        guard let controller = Self(nibName:Controller.nibNameIdentifier,bundle:nil) as? Controller else {
            fatalError("Could not dequeue cell with identifier: \(Controller.nibNameIdentifier)")
        }

        return controller
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在尝试创建VC实例时,

 let recommendationVC :RecommendationVC = UIViewController.instantiate()
Run Code Online (Sandbox Code Playgroud)

接收错误 无法推断通用参数“控制器”

这种方法有什么问题?

inv*_*doo 7

添加UIViewController的扩展

extension UIViewController {
    static func instantiateFromNib() -> Self {
        func instantiateFromNib<T: UIViewController>(_ viewType: T.Type) -> T {
            return T.init(nibName: String(describing: T.self), bundle: nil)
        }

        return instantiateFromNib(self)
    }
}
Run Code Online (Sandbox Code Playgroud)

然后像那样使用

让myViewController = MyViewController.instantiateFromNib()

  • 谢谢!这对我有用,尽管我没有使用内部功能;我只是直截了当地说`return self.init(nibName:String(describing:self),bundle:nil);` (2认同)