Swift Text Label Nil Even With Default Value -


this driving me crazy. function updatetextview() being called, verified print statements, not setting label in view controller, , print statements label returning nil though has default value set visible when app loaded. whats more perplexing set test button call function separately, , when call test(), label updates properly.

class goaldetailviewcontroller: uiviewcontroller, textdelegate {      @ibaction func test(sender: anyobject) {         updatetextview()     }      func updatetextview() {         print(goalsummarytextbox?.text)         print("delegate called")         self.goalsummarytextbox?.text = goalsdata.summarytext         print(goalsummarytextbox?.text)     }      @iboutlet weak var goaltitle: uilabel?     @iboutlet weak var goalcreationdate: uilabel?     @iboutlet weak var goalsummarytextbox: uitextview?      override func viewdidload() {         super.viewdidload()          goalsummarytextbox?.text = goalsdata.summarytext     } } 

updatetextview() being called through delegate method after pop different view controller, can seen below:

class texteditviewcontroller: uiviewcontroller {     var textdelegate: textdelegate?      @iboutlet weak var textview: uitextview?      func configureview() {         navigationitem.title = "edit description"         navigationitem.setrightbarbuttonitem((uibarbuttonitem(barbuttonsystemitem: .done, target: self, action: "segue")), animated: true)     }      func segue() {         textdelegate = goaldetailviewcontroller()          if let text = textview?.text {                 goalsdata.summarytext = text         }          textdelegate?.updatetextview()                 self.navigationcontroller?.popviewcontrolleranimated(true)     } } 

the line causing issue in texteditviewcontroller below:

textdelegate = goaldetailviewcontroller() 

what line creates new instance of goaldetailviewcontroller, , sets delegate texteditviewcontroller. but, want original instance of goaldetailviewcontroller delegate. why seeing logs when popping texteditviewcontroller, since executing other instance (which wasn't part of view hierarchy). explains why iboutlets nil when stepping through updatetextview() on delegate call, , button added updates text properly.

the solution make delegate connection in prepareforsegue method:

override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     if let destination = segue.destinationviewcontroller as? texteditviewcontroller {         destination.textdelegate = self     } } 

add above code goaldetailviewcontroller.

edit:

the below code ensure problem not happen in future. change delegate's definition to:

weak var textdelegate: textdelegate? 

and change protocol to:

protocol textdelegate: class {     func updatetextview() } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -