ios - Dynamically instantiating custom UIView's from XIB issue (what's wrong with this code)? -


what wrong following code, it's seems have endless loop. comes through init:frame, commoninit, xib loading line triggers entering again through init:coder etc.

two areas of question:

a) how instantiate avoid problem (i.e. want use xib layout, dynamically creating/position multiple of these on parent view in code)

b) setting self.label.text problematic seems self.label (a uilabel linked xib) hasn't been setup @ point, hence nil. dynamically, when want create little custom uiview via xib, add subclass, set value of label how do this?

import uikit

class directioninfoview: uiview {      @iboutlet weak var label: uilabel!      func commoninit() {         let viewname = "directioninfoview"         let view: directioninfoview = nsbundle.mainbundle().loadnibnamed(viewname, owner: self, options: nil).first as! directioninfoview  // <== exc_bad_access (code=2...)         self.addsubview(view)         view.frame = self.bounds          self.label.text = "testing 123"     }      override init(frame: cgrect) {         super.init(frame: frame)         commoninit()     }      required init?(coder adecoder: nscoder) {         super.init(coder: adecoder)         commoninit()     }  } 

usage:

 let newinfoview = directioninfoview(frame: self.mapview.bounds)  myexitingview.addsubview(newinfoview) 

this seems work:

import uikit  class directioninfoview: uiview {     @iboutlet weak var label: uilabel!      func commoninit() {         self.layer.borderwidth = 5         self.layer.cornerradius = 25     }      override init(frame: cgrect) {         super.init(frame: frame)         commoninit()     }      required init?(coder adecoder: nscoder) {         super.init(coder: adecoder)         commoninit()     }  } 

usage:

let directioninfoview : directioninfoview = nsbundle.mainbundle().loadnibnamed("directioninfoview", owner: self, options: nil).first as! directioninfoview mapview.addsubview(directioninfoview) directioninfoview.label.text = "it worked!" 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -