uitableview - keep UISwitch state of a prototype cell when changing ViewControllers -


i have uitableviewcell subclass have connected uiswitch custom prototype cell. (data passed in table view previous viewcontroller)

when change view controllers state of uiswitchchanges default need way remember state of switch while navigating through app. have searched few other topics, nothing seemed fall in case.

here code:

import uikit  class tableviewcell: uitableviewcell {  @iboutlet weak var mystaticswitch: uiswitch! @iboutlet weak var celllabel: uilabel! @ibaction func myswitch(sender: uiswitch) {      if mystaticswitch.on {     self.celllabel.text = "on"     //do other things     }     else {         self.celllabel.text = "off"     //do other things     } }    override func awakefromnib() {     super.awakefromnib()      // initialization code }  override func setselected(selected: bool, animated: bool) {     super.setselected(selected, animated: animated)      // configure view selected state }  } 

you should have model object used determine state of ui representing it. object can saved disk, persisted core data, or synced remote web service.

when next encounter need display ui related object, can check properties determine show. ui action method updates model representation:

@ibaction func myswitch(sender: uiswitch) {     mymodelobject.enabled = mystaticswitch.on } 

the ui (e.g., table cell) looks changes enabled , updates celllabel needed.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -