Thursday 15 January 2015

ios - How To Init A UIView That Exists In Interface Builder But Also Requires Init In Code -



ios - How To Init A UIView That Exists In Interface Builder But Also Requires Init In Code -

here view class

class v_takephoto:uiview{ var _takephotocallback:(iimage)->void? required init(coder adecoder: nscoder) { super.init(coder: adecoder) _takephotocallback = nil } @ibaction func takephoto(sender: anyobject) { println("here go!") } func initwithcameracallback((iimage)->void) { } }

this class uiview subclass. in interface builder selected viewcontroller class, , selected view object. assigned v_takephoto view object.

in viewcontroller class, assigned c_takephoto class, want init v_takephoto class.

as can see, want have callback variable gets passed @ run time. however, because view getting initialized interface builder, init(coder) getting called first.

as stands right seems hacky need have 2 init functions. 1 interface builder calls it, 1 time again when viewcontroller inits view callback. have number of variables, , need pre-init them in init(coder) phone call re-init them 1 time again when viewcontroller calls 'true' init on v_photoclass. seems hacky me, there must clean 'correct' way this.

can suggest cleaner way handle situation have variables , need init view despite there beingness init(coder) phone call interface builder?

i suggest creating function in v_takephoto , phone call in both v_takephoto's init(coder) , viewcontroller's viewdidload(), :

in v_takephoto :

required init(coder adecoder: nscoder) { super.init(coder: adecoder) specialinit() } func specialinit() { // of view initialization }

in view controller :

@iboutlet weak var takephotoview: v_takephoto! override func viewdidload() { super.viewdidload() // method called after view controller has loaded view hierarchy memory. takephotoview.specialinit() // re-init }

ios xcode uiview uiviewcontroller swift

No comments:

Post a Comment