views:

53

answers:

2

Okay this is a design question. I, like many, are still learning Objective-C and Cocoa, and am a little rusty to boot. Anyway, here is the question:

Assume I have a ViewController class 'A'.
Assume I have a "Camera" class 'B', which is a singleton.
Assume I have a UILabel class 'C'.

The ViewController 'A' has knowledge of the "Camera" 'B' and can always easily get a reference to it by asking for B.sharedInstance

I set up a timer in 'A' which repeatedly calls a method in the "Camera" class 'B'.

Every time the method in 'B' is called by the timer, I also want to send a message to the UILabel 'C' as well....

Only, the "Camera" 'B' doesn't have any knowledge of the UILabel, so I can't update the UILabel in the camera's method which is being called by the timer.

The value the UILabel displays is from the "Camera". It needs to get that information immediately after the timer has called the timer-invoked method in the "Camera" class.

How do I design this properly so the UILabel 'C' can be properly informed of the changes in the "Camera" every time the timer calls its method.

If you're still with me, thank you for reading. Any advice would certainly be instructive. Thanks in advance for any help you can offer.

I realize I could pass the UILabel as an argument to the camera in the timer call, but that seems really ugly. And I could make the UILabel a singleton, and then the camera can call C.sharedInstance, which still makes sense in this program but that seems WAY wrong.

What is the right way to do this?

A: 

I think it depends on where the UILabel is situated. It's view controller should handle any updates to the label. So if 'A' is the view controller of the UILabel (the label is part of the 'A' view) then 'A' should take care of the label.

You can use a few different methods of getting the data from the camera. One of the easiest would be to create a CameraDelegateProtocol and make 'A' the delegate of 'B'. Then 'B' can send a delegate message to 'A' with perhaps some parameters, and 'A' can then update the UILabel.

If you want to update several labels throughout the app, you could use a notification system.

When using a singleton like 'B' I think a notification system would be a lot nicer, because since multiple objects can reference and use 'B', multiple objects should be able to receive updates.

Making 'C' a singleton seems like a really bad idea since it makes a very tight coupling between 'B' and 'C'. Using delegates/notifications or something alike creates louse coupling, which is great for reusability of the classes.

Rengers
A: 

Sounds to me like your timer should invoke a method in your view controller, which then sends the correct message to the camera and updates the label. The camera should expose the info that needs to be displayed in the label as a property.

Ole Begemann