I'm trying to use inheritance in UIViewController
where base class has XIB in storyboard. I'm troubling with initiating different children classes with that XIB.
I cannot use XIB file separately instead of storyboard because it has static UITableViewCells
.
So here is how I'm managing it currently using object_setClass
method in SWIFT:
Parent Class:
class MeetingViewController: UITableViewController {
private(set) var project: CDProject?
private(set) var meeting: CDMeeting?
static func initWith(project: CDProject?, meeting: CDMeeting?) -> MeetingViewController {
let storyboard = UIStoryboard(name: "Meeting", bundle: nil)
let meetingVC = storyboard.instantiateViewController(withIdentifier: "MeetingVCId") as! MeetingViewController
meetingVC.project = project
meetingVC.meeting = meeting
return meetingVC
}
}
Child Class-1:
class MeetingInfoViewController: MeetingViewController {
static func initWith(meeting: CDMeeting) -> MeetingInfoViewController {
let meetingVC = MeetingViewController.initWith(project: nil, meeting: meeting)
object_setClass(meetingVC, MeetingInfoViewController.self)
return meetingVC as! MeetingInfoViewController
}
}
Child Class-2:
class MeetingCreateViewController: MeetingViewController {
static func initWith(project: CDProject) -> MeetingCreateViewController {
let meetingVC = MeetingViewController.initWith(project: project, meeting: nil)
object_setClass(meetingVC, MeetingCreateViewController.self)
return meetingVC as! MeetingCreateViewController
}
}
This is working very fine, not any issue; but I'm just curious if:
1 there's any other way to handle this scenario?
2 this approach has any side-effects?
3 this can be improved somehow?
Also feel free to point out other things than initialisation!