swift - Use Delegate or Notifications? In delegate example label is nil -


i found lot delegates vs notifications. don't understand difference yet.

in project have class working in background , update ui. have used notifications that.

now created example: when click in firstviewcontroller (fvc) on button1, delegate function starts. when click in fvc on button2, jump fvc secondviewcontroller (svc). can click button1 many times , someday click second button.

but label in svc nil.

fvc:

protocol test { func somefunction(somestring: string) }  class firstviewcontroller: uiviewcontroller {  var secviewdel: secondviewcontroller = secondviewcontroller()  //...  @ibaction func clickme(sender: anyobject) {     secviewdel.mydelegate = self     secviewdel.somefunction("sendastring") }  override func viewdidload() {     super.viewdidload()    //do stuff  } } 

svc:

class secondviewcontroller: uiviewcontroller,test {    var mydelegate:firstviewcontroller? = nil   func somefunction(somestring: string) {      print("\(somestring)") //sendastring      if let lblnil = lbl {         lblnil.text = somestring      } else {         print("lbl nil") //lbl ist nil     }   }  @iboutlet weak var lbl: uilabel!   override func viewdidload() {     super.viewdidload()     //do stuff  }    } 

sendastring

lbl nil

for sure can solve problem in example, when use segue here. don't want jump svc.

a lot of people don't notification much. here found out delegates used 1:1 relations , notifications 1:n relations. (i find no "official source" - know?)

i in example 1:1 relation. fvc update svc x times. someday user jumps svc , see actual data.

in project it's little bit more complicated. there class running in background , new data server. everytime when new data arrives class updates main-screen. notification works. delegates same error in example: ui elements nil. how can fix example?

for me relation 1:1 again.

i want understand in cases use delegate or notification.

thank


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -