ios - Double Initialization Of UIViewController's Subclass Member in Swift -



ios - Double Initialization Of UIViewController's Subclass Member in Swift -

i wanted create custom container view controller , added members subclass of uiviewcontroller. when seek init app delegate using next code:

self.window = uiwindow(frame: uiscreen.mainscreen().bounds) self.window?.rootviewcontroller = customcontainerviewcontroller() self.window?.makekeyandvisible()

all members in customcontainerviewcontroller initialized twice.

here customcontainerviewcontroller's code:

class customcontainerviewcontroller: uiviewcontroller { allow tabbar = customtabbar() override init() { super.init() } required init(coder adecoder: nscoder) { super.init(coder: adecoder) } override init(nibname nibnameornil: string?, bundle nibbundleornil: nsbundle?) { super.init(nibname: nibnameornil?, bundle: nibbundleornil?) } }

here customtabbar's code:

class customtabbar: uiview { override init(){ println("init") super.init() } override init(frame: cgrect) { println("initwithframe:") super.init(frame: frame) } required init(coder adecoder: nscoder) { println("initwithcoder:") super.init(coder: adecoder) } }

whenever init customcontainerviewcontroller app delegate using code mentioned, prints "init", "initwithframe" twice.

incorrect designated initializer used.

uiviewcontroller has 1 designated initializer init(nibname nibnameornil: string?, bundle nibbundleornil: nsbundle?).

as comment says

the designated initializer. if subclass uiviewcontroller, must phone call super implementation of method, if aren't using nib. (as convenience, default init method you, , specify nil both of methods arguments.) in specified nib, file's owner proxy should have class set view controller subclass, view outlet connected main view. if invoke method nil nib name, class' -loadview method effort load nib name same view controller's class. if no such nib in fact exists must either phone call -setview: before -view invoked, or override -loadview method set views programatically.

so whenever override init() method of uiviewcontroller, 1 time phone call super, uiviewcontroller's implementation phone call init(nibname nibnameornil: string?, bundle nibbundleornil: nsbundle?) on behalf of you. members in uiviewcontroller's subclass initialized twice.

to solve problem, utilize next code in app delegate

self.window = uiwindow(frame: uiscreen.mainscreen().bounds) self.window?.rootviewcontroller = customcontainerviewcontroller(nibname: nil, bundle: nil) self.window?.makekeyandvisible()

and never phone call init() method of uiviewcontroller or override method in subclasses.

ios cocoa-touch swift

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -