I have class and a view that are not connected in any way. I need to create an event such that when it's raised I want to change a label's text in another view. What would be the most correct and elegant way to do this ?
-
1Can you provide more information about the class and the view? – Masterfego Oct 12 '15 at 11:02
-
In Class A add an observer for that event and from class B just post a notification for that observer. Simple. If need some code then let me know. – if-else-switch Oct 12 '15 at 11:10
5 Answers
It sounds like you are looking for an Event Bus AKA Publish/Subscribe.
What this means is that your controller can post "events" and your views can become a "listeners" to these events. And so when an event is called, your views react.
Take a look at this open-source library for a simple way to do this.

- 2,887
- 1
- 19
- 30
There are lots of ways to achieve this. You can use protocols, notifications, key value observing etc. If you use Reactive Cocoa you can use subjects, signals to subscribe them as well.
Since they are not directly connected to each other and not coupled, using delegate is not the best option.
NSNotification always works but you need to be careful adding and removing observers in proper places. You might end up crashing your app. Notifications are also really hard to debug since there is no direct connection but only observers and publishers. Try to centralize it and limit it to avoid side effects.
KVO is also possible for observing a state of another object and reflect this to another object. This is safer than using normal notifications but still highly coupled.
I would try to relate them in another object. They can talk through via another helper object. Also this is better if you want to improve that connection between as well. You can keep the track easily and change related values.

- 442
- 4
- 15
Option 1: You could use the Delegate pattern.
Create a delegate protocol. Your view controller class should conform to that delegate. Whenever you need to trigger the event, just call that delegate method from your class. That will also trigger the function in your view controller class, where you update the label's text.
Check this link for a well explained tutorial.
Option 2: Use KVO (Key value observing)
Check this link for more about KVO.
Comment if you need further code examples.

- 1,396
- 1
- 9
- 21
-
Class and view does not have reference to each other. There isn't any instances of the class or the view which they can talk through delegate. – erenkabakci Oct 12 '15 at 11:09
-
You're right if that is the case. But the question seemed a bit ambiguous to me. So I'm leaving it as an option until further clarification is provided. – mohonish Oct 12 '15 at 11:16
Seems like you're describing exactly what Reactive Programming is all about. When your model change, you want your views to be updated accordingly. There are two great libs that can handle this for you:
- ReactiveCocoa (RAC 4) if you want it to be compatible with Swift2
- RxSwift which is a little more lightweight but very powerful as well
I suggest you dig into this and start enjoying the Reactive Programming paradigm

- 9,703
- 6
- 40
- 45
If your classes have no reference to each other and it's really hard to connect them then use UINotificationCenter: