Please excuse me if its repeated topic.
I usually write my apps without storyboards, and put views creation into "viewDidLoad", like:
class LoginVC: UIViewController {
var view1: UIView!
var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
loadStaticViews()
}
func loadStaticViews() {
view1 = UIView()
label1 = UILabel()
view.addSubview(view1)
view1.addSubview(label1)
// constraints...
}
}
And now I want to try MVVM pattern in my next app, and just not sure where to put views creation. Now I think about something like that:
class LoginVCViews {
static func loadViews<T, T1, T2>(superview: UnsafeMutablePointer<T>, view: UnsafeMutablePointer<T1>, label: UnsafeMutablePointer<T2>) {
guard let superview = superview.pointee as? UIView else { return }
let v = UIView()
let l = UILabel()
superview.addSubview(v)
v.addSubview(l)
// constraints...
view.pointee = v as! T1
label.pointee = l as! T2
}
}
class LoginVC: UIViewController {
private var view1: UIView!
private var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
LoginVCViews.loadViews(superview: &view, view: &view1, label: &label1)
}
}
What do you think ? I'm not familiar with UnsafeMutablePointer very well and don't sure there wont be a some problems. And how much it's ugly ?
Maybe you should try the fully object oriented path. A view composition looks something like that:
// reusable protocol set
// reusable functionality (no uikit dependency)
// reusable UI (uikit dependency)
// a viewcontroller
// and now the business logic of a screen (not reusable)
Usage in AppDelegate:
Note:
In my opinion it's the best way to code, but most people don't do it like that.