1

I am fairly new to Objective-C, but haven't been able to find the answer I am looking for yet.

I am building a practice app which counts down days till specific events (christmas). It all works well, but I wanted to implement a simple way to change the background image (created from storyboards). The current setup of the app is a basic utility app, with the alternate view containing a UISegmentedControl that will set which background image will be displayed behind the countdown label.

I know I can just push separate views, but that seems quite inefficient. I am fairly sure I can use NSNotifications to update the background image, but am still unclear after reading the documentation how to implement it.

  • just not getting it - is the problem that you want a way to communicate from the configuration view to the countdown view to change it's background? Or, are you looking for it to change automatically based on timer (day change etc...) – bryanmac Nov 08 '11 at 00:35
  • Apologies for not being more clear. Just wanted a way to communicate from the configuration view to the countdown view, changing the background depending on which button the user presses in the segmented button. – modal escape Nov 08 '11 at 00:57

1 Answers1

0

One way is to use delegates. A delegate is a callback. That avoids one view passing a reference to another view and having it poke (introduces coupling). Instead, one provides a callback via a formal protocol and delegate to the other.

Here's an SO question that covers it:

What exactly does delegate do in xcode ios project?

Another option is to have a shared model. Using a singleton pattern, multiple views can share a model (data and operations) and you can use NSNotificationCenter to send notifications. When the other view gets the notification that the background changed, it can query the model and update the background.

Related posts:

Giving notification to another class with NSNotificationCenter

How to share an object with the entire project?

As far as the pros and cons go, basically a notification is good when you want to broadcast a change to multiple views. Delegation is good when two items need to communicate directly with some abstraction.

NSNotificationCenter vs delegation( using protocols )?

Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99