ios - Can a UIView subclass create other custom view instances? -


i have uiview subclass called lifecounter, of want create 1 each player. able add 2 views main.storyboard, set class lifecounter through attributes panel , create multiple instances way, connect view controller, change properties, etc.

what thinking creating larger view, gameheader, hold lifecounters , other supplementary information such time, game reset button, etc. gameheader uiview subclass, can't draw lifecounter views in simulator , have no idea why.

gameheader view dragged storyboard, , given it's class attributes panel.

gameheader.swift

import uikit  class gameheader: uiview {  // mark: properties  // frame let topframe = cgrect(x: 0, y: 0, width: 320, height: 200)  // mark: initlization required init() {     super.init(frame: topframe)      // adds player 1 counter     let playeronecounter = lifecounter()     addsubview(playeronecounter)  }  required init(coder adecoder: nscoder) {     // calls super class (uiview) initializer     super.init(coder: adecoder) }  } 

lifecounter.swift

import uikit  class lifecounter: uiview {  // mark: propterties  // starting life total var lifetotal = 20 {     didset {         // updates layout whenever lifetotal updated         setneedslayout()     } }  // creates ui labels // created views need defined frame sit // neccesary outside of init? there better way? var counter = uilabel(frame: cgrect(x: 0, y: 20, width: 100, height: 90)) var playername = uilabel(frame: cgrect(x: 0, y: 0, width: 100, height: 40)) var winner = ""    // mark: initlization  // first init. lifecounter takes frame parameter, adds labels etc. init() {     super.init(frame: cgrect(x: 0, y: 0, width: 200, height: 100))     self.addlifecounter() }  required init(coder adecoder: nscoder) {     // calls super class (uiview) initializer     super.init(coder: adecoder) }  func addlifecounter() {     print("addlifecounter running")     // styles life counter label     counter.textcolor = uicolor.blackcolor()     counter.textalignment = .center     counter.font = uifont.boldsystemfontofsize(72)     counter.text = string(lifetotal)      // styles playername label     playername.text = "player name"     playername.textalignment = .center       // button     let minusbutton = uibutton(frame: cgrect(x: 5, y: 110, width: 40, height: 40))     let plusbutton = uibutton(frame: cgrect(x: 55, y: 110, width: 40, height: 40))      minusbutton.backgroundcolor = uicolor.redcolor()     plusbutton.backgroundcolor = uicolor.bluecolor()      // button action     minusbutton.addtarget(self, action: "minuslife:", forcontrolevents: .touchdown)     plusbutton.addtarget(self, action: "pluslife:", forcontrolevents: .touchdown)      addsubview(playername)     addsubview(counter)     addsubview(minusbutton)     addsubview(plusbutton) }   // mark: button actions func minuslife(minusbutton: uibutton) {     lifetotal -= 1     counter.text = string(lifetotal) }  func pluslife(plusbutton: uibutton) {     lifetotal += 1     counter.text = string(lifetotal) }  } 

init(coder:) initializer called when view created storyboard or nib. if move code creates , adds lifecounter instances there, should work.

a strategy make more reusable create setup method called both initializers run whether comes storyboard/nib or instantiated programmatically.

class gameheader: uiview {     let topframe = cgrect(x: 0, y: 0, width: 320, height: 200)      required init() {         super.init(frame: topframe)         self.setupsubviews()     }      required init(coder adecoder: nscoder) {         super.init(coder: adecoder)         self.setupsubviews()     }      func setupsubviews() {         let playeronecounter = lifecounter()         addsubview(playeronecounter)     }  } 

Comments