swift - Setting delegate of another class with screen view to self -


i'm new @ ios programming. have setup:

viewcontroller view on ib, class viewcontroller

secondcontroller view on ib, class secondcontroller

i have protocol:

protocol secondcontrollerdelegate {     func getsomething() -> string } 

and have delegate variable on secondcontroller:

class secondcontroller: uiviewcontroller {     var delegate: secondcontrollerdelegate?     @iboutlet weak var labelstatus: uilabel!      override func viewdidload() {         super.viewdidload()     }      @ibaction func buttontouch(sender: anyobject) {         labelstatus.text = delegate?.getsomething()     }      func try () {         labelstatus.text = "testing"     } } 

now, according hints everywhere, in order can call delegate?.getsomething() @ secondcontroller.buttontouch(), need set on viewcontroller:

class viewcontroller: uiviewcontroller, secondcontrollerdelegate {     override func viewdidload () {         super.viewdidload()         secondcontroller.delegate = self     }     func dosomething () -> string {         return "testing"     } } 

but generates error 'secondcontroller.type' not have member named 'delegate'.

some other websites say:

class viewcontroller: uiviewcontroller, secondcontrollerdelegate {     var secondcontroller = secondcontroller()     override func viewdidload () {         super.viewdidload()         secondcontroller.delegate = self     }     func dosomething () -> string {         return "testing"     } } 

with this, there no error. if on second screen should call delegate, doesn't call delegate, secondcontroller 2 different objects (one created storyboard, 1 created manually within viewcontroller), i.e. labelstatus should have changed "testing", doesn't change @ all. changes if function try() called. how supposed this?

edit: forgot mention used navigationcontroller, , segue transition first screen second screen.

because try learn how build delegate in swift, have written plain delegate example below

protocol secondviewcontrollerdelegate {     func didreceiveinformationfromsecondviewcontroller (information: string) }  class viewcontroller: uiviewcontroller, secondviewcontrollerdelegate {      func opensecondviewcontroller () {         if let secondviewcontrollerinstance: secondviewcontroller = storyboard?.instantiateviewcontrollerwithidentifier("secondviewcontroller") as? secondviewcontroller {             secondviewcontrollerinstance.delegate = self             navigationcontroller?.pushviewcontroller(secondviewcontrollerinstance, animated: true)         }     }      func didreceiveinformationfromsecondviewcontroller(information: string) {         ////here information, after sendinfotoviewcontroller() has been executed     }  }   class secondviewcontroller: uiviewcontroller {      var delegate: secondviewcontrollerdelegate?      func sendinfotoviewcontroller () {         delegate?.didreceiveinformationfromsecondviewcontroller("this ist information")     }  } 

update

following same thing in using storyboard segues

override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {         if let secondviewcontrollerinstance: secondviewcontroller = segue.destinationviewcontroller as? secondviewcontroller {             secondviewcontrollerinstance.delegate = self         }     } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -