ios - UIscrollview delegation -


i trying pass data between 2 view controllers in uiscrollview. trying use delegation send data between viewcontroller1 , viewcontroller2. delegate viewcontroller, while delegator viewcontroller1 , viewcontroller2.

in code posted below, when switch in viewcontroller1 toggled, makes switch in viewcontroller2 put "off" state. keep on getting

fatal error: unexpectedly found nil while unwrapping optional value

error when run it, have no clue causing problem. ideas why?

below viewcontroller contains uiscrollview , subviews/childviews

 import uikit  class viewcontroller: uiviewcontroller, testing {   var vc1 = viewcontroller1(nibname: "viewcontroller1", bundle: nil) var vc2 = viewcontroller2(nibname: "viewcontroller2", bundle: nil)    @iboutlet weak var scrollview: uiscrollview!  func test1() {     vc2.switch2.on = false }  override func viewdidload() {     super.viewdidload()        self.addchildviewcontroller(vc1)     self.scrollview.addsubview(vc1.view)     vc1.didmovetoparentviewcontroller(self)        var frame1 = vc2.view.frame     frame1.origin.x = self.view.frame.size.width     vc2.view.frame = frame1      self.addchildviewcontroller(vc2)     self.scrollview.addsubview(vc2.view)     vc2.didmovetoparentviewcontroller(self)      self.scrollview.contentsize = cgsizemake(self.view.frame.size.width * 2, self.view.frame.size.height); }  override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. } 

}

here viewcontoller1 code

    protocol testing{ func test1() }     class viewcontroller1: uiviewcontroller {  var delegate:testing?  @iboutlet weak var switch1: uiswitch!  override func viewdidload() {     super.viewdidload()     let vc = viewcontroller()     self.delegate = vc     // additional setup after loading view. }  override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }  @ibaction func switch1toggled(sender: anyobject) {     delegate?.test1()  }  /* // mark: - navigation  // in storyboard-based application, want little preparation before navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     // new view controller using segue.destinationviewcontroller.     // pass selected object new view controller. } */  } 

and here viewcontroller 2 code

import uikit  class viewcontroller2: uiviewcontroller { @iboutlet weak var switch2: uiswitch!  override func viewdidload() {     super.viewdidload()      // additional setup after loading view. }  override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }  @ibaction func switch2toggled(sender: anyobject) { }  /* // mark: - navigation  // in storyboard-based application, want little preparation before navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     // new view controller using segue.destinationviewcontroller.     // pass selected object new view controller. } */ 

}

sorry long post, have been stuck week on how change state of switch toggling switch in class, , efficient way found

try this:

viewcontroller1

class viewcontroller1: uiviewcontroller {    let defaults = nsuserdefaults.standarduserdefaults()    let switch1key = "view1switch"    override func viewwillappear(animated: bool) {     view1switch.on = defaults.boolforkey(switch1key)   }    @iboutlet weak var view1switch: uiswitch!    @ibaction func view1switchchanged(sender: uiswitch) {     defaults.setbool(view1switch.on, forkey: switch1key)   }  } 

viewcontroller2

class viewcontroller2: uiviewcontroller {    let defaults = nsuserdefaults.standarduserdefaults()    let switch1key = "view1switch"    override func viewwillappear(animated: bool) {     view2switch.on = defaults.boolforkey(switch1key)   }    @iboutlet weak var view2switch: uiswitch!    @ibaction func view2switchchanged(sender: uiswitch) {     defaults.setbool(view2switch.on, forkey: switch1key)   }  } 

this method syncs state of 2 uiswitches using viewwillappear , nsuserdefaults. basic thought pattern save state of switch nsuserdefaults when either viewcontroller1 or viewcontroller2 instantiated view1switch or view2switch outlet's .on property set saved value.

caveats:

  1. the first value switch when viewcontroller1 instantiated (in first app run) off because boolforkey returns false when there no saved value. can hacked using view1switch.on = true directly after view1switch.on = defaults.boolforkey(switch1key)

  2. this method makes switches have same value. in order make them have different values, can use ! operator in viewcontroller2 view2switch.on = !defaults.boolforkey(switch1key). way switch 1 opposite of switch 2.

i recommend method on delegation because, while delegation powerful, power doesn't seem needed here. if have questions please ask! :d


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -