ios - Subclassing UITableViewController in Swift -
i subclassed uitableviewcontroller in swift , added custom initializer:
init(schedulecontroller: schedulecontroller) { self.schedulecontroller = schedulecontroller super.init(style: uitableviewstyle.plain) self.title = "routes" } now when start app crashes with:
fatal error: use of unimplemented initializer 'init(nibname:bundle:)' class 'shuttle_plan.routesviewcontroller' how can solve without adding initializer?
maddening, isn't it? error message tells do: if going call super.init(style:), then must implement init(nibname:bundle:), if there call super:
override init(nibname nibnameornil: string?, bundle nibbundleornil: nsbundle?) { super.init(nibname:nibnameornil, bundle:nibbundleornil) } this, however, raises question of properties. swift won't let implement init(nibname:bundle:) unless initializes uninitialized properties.
one obvious solution make properties optionals; implementation shown above legal. in
init(schedulecontroller:), have initialize properties after callingsuper.init.less dramatically, make property
varnot optional. initialize multiple times: beforesuper.initin initializers, , aftersuper.initininit(schedulecontroller:).
none of those, however, solution use in own code. in code, not call super.init(style:), doing — call super.init(nibname:bundle:) instead. works because:
the default style
.plain, wanteven if want
.grouped, can specify in associated nib file
the big advantage of approach permits properties non-optional let, do.
Comments
Post a Comment