0

I found a lot about Delegates vs Notifications. But I don't really understand the difference yet.

In my project I have a class which is working in the background and update the UI. I have used notifications for that.

Now I created an example: When I click in FirstViewController (FVC) on button1, then the delegate function starts. When I click in FVC on button2, I jump from FVC to the SecondViewController (SVC). I can click button1 many times and someday I click the second button.

But the label in the SVC is always 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 is nil") //lbl ist always nil
    }

 }
 @IBOutlet weak var lbl: UILabel!

 override func viewDidLoad() {
    super.viewDidLoad()
    //do stuff
 }   
}

sendAString

lbl is nil

For sure I can solve my problem in my example, when I use a segue like here. But I don't want always jump to SVC.

A lot of people say that they don't like notification so much. Here I found out that delegates are normally used for 1:1 relations and notifications for 1:n relations. (I find no "official source" for that - does somebody know?)

I would say that in my example it is a 1:1 relation. The FVC could update the SVC x times. Then someday the user jumps to the SVC and see the actual data.

In my project it's a little bit more complicated. There is a class running in the background and get always new data from a server. Everytime when the new data arrives the class updates the main-screen. With notification it works. With delegates I get the same error like in my example: The UI elements are always nil. How can I fix my example?

For me the relation is 1:1 again.

I want to understand in which cases I use delegate or notification.

Thank you

Community
  • 1
  • 1
kuzdu
  • 7,124
  • 1
  • 51
  • 69
  • You won't be able to update a label when the view isn't loaded, because it doesn't exist at this point. You will have to store the value in some global variable for example and set the label when loading the second view controller in `viewDidLoad` or something – konrad.bajtyngier Jan 28 '16 at 10:11
  • 1
    To add to what @konrad.bajtyngier said: if you have `SecondViewController` setup in Storyboard, then `secViewDel` is not referring to the Storyboard instance. – Stuart Breckenridge Jan 28 '16 at 12:36
  • okay, but when I store my value in a global variable. Then I don't need delegates or? – kuzdu Jan 29 '16 at 15:50

0 Answers0