Is it possible to use generic UIViewController subclass and use it with XIB? For example
import UIKit
protocol ViewModel {
var title: String { get }
}
class ViewController<ViewModelType: ViewModel>: UIViewController {
let viewModel: ViewModelType
init(viewModel: ViewModelType, nibName: String) {
self.viewModel = viewModel
super.init(nibName: nibName, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
title = viewModel.title
}
}
class TestViewModel: ViewModel {
let title: String
init(title: String) {
self.title = title
}
}
class TestViewController: ViewController<TestViewModel> {
init(viewModel: TestViewModel) {
super.init(viewModel: viewModel, nibName: "TestViewController")
}
}Now the view controller is not loaded correctly from the XIB. I've been able to get it working by first defining TestViewController as UIViewController and then setting the outlest in interface builder. But should this even work?