1

I'm creating one UIViewController that operates a calculator interface, while another UIViewController deals with drawing a graph.

The calculator prompts the graph to redraw itself when the user pushes the graph button. The graph then performs a call back to a protocol method on the calculator to get the y value for each given x coordinate.

I'm wondering what the standard way of handling this kind of communication is? I feel like I should be able to call something akin to setNeedsDisplay on a UIViewController (I'm aware this is actually only a UIView method), but I have a feeling I'm going to have to write another protocol with an similar method. Is there some kind of updateViewController method I'm missing?

Ideally I'd like my Calculator UIViewController to only know the graph as a UIViewController while the graph knows the Calculator through a protocol I define.

Lockyer
  • 1,311
  • 1
  • 13
  • 30

3 Answers3

1

To my knowledge there is nothing in UIViewController that does exactly what you want. You'll probably need to do some kind of protocol as you suggest and then [yourViewController.view setNeedsDisplay:].

Hope that helps.

mdominick
  • 1,319
  • 1
  • 10
  • 21
1

There are many ways of doing it and this has been asked here before. For example:

iPhone how to pass data between several viewcontrollers

In your particular case, I would use an NSNotification. Subscribe to it in the viewController that needs to redraw and post a notification from the other one.

Community
  • 1
  • 1
EricS
  • 9,650
  • 2
  • 38
  • 34
1

You have several methods to do that.

  1. Target/Action
  2. Delegates
  3. NSNotificationCenter
  4. KVO (Key Value observing)

In your case, KVO seems to be the best option. Basically, what you graph draws depends on your calculator interface (if I understand correctly) so you are observing some variables (read keys) for change. Look at this document to learn more.

KVO can let you optimize graph drawings as well because you can get previous value, new value etc. Infact, by definition:

Key-value observing is a mechanism that allows objects to be notified of changes to specified properties of other objects.

zakishaheen
  • 5,551
  • 1
  • 22
  • 29
  • Key-value observing sounds interesting, I've seen `NSNotificationCenter` mentioned elsewhere but I like what that doc says about `KVO`: "Unlike notifications that use `NSNotificationCenter`, there is no central object that provides change notification for all observers. Instead, notifications are sent directly to the observing objects when changes are made." That seems less "singleton like", which I think is a good thing! – Lockyer Oct 25 '11 at 10:39
  • Yes in this case, sure. But Say if you had tabs. A view controller within a tab doesn't know about a view controller in another tab. There, KVO would not be possible (or possible Uglily). Notification Center would be the best option there. Btw, if you think this is the right answer, you can click the check mark :) – zakishaheen Oct 25 '11 at 11:18
  • After a bit more reading I think Target/Action is more for `UIViewController`-`UIView` communication, and `NSNotificationCenter` and `KVO` seem more suited toward inter-model communication. I think after all that I'll probably go with Delegates/Protocols, but it's good to know what other facilities IOS provides. – Lockyer Oct 26 '11 at 01:10