views:

31

answers:

1

A beginner's question:

If, in your nib, you have the File's Owner linked to the ViewController class, and you also have a NSObject-derived class, how do you communicate between the ViewController class and the NSObject class within code?

For instance, suppose ScientificCalculatorView.xib looks like this

File's Owner  (class: ScientificCalculatorViewController)
FirstResponder
View
Calculator  (an object that has been linked to the Calculator class)

Obviously, I'd want Calculator to be reusable, so it could be used with a NormalCalculatorViewController or something like that. So that UI and the calculator code are separate. Does Calculator even need to be in the nib?

It's a beginners question, but I'm just trying to get my head around it.

+1  A: 

There are two ways (at least) to handle this:

  1. Set up an outlet in your ScientificCalculatorViewController defined: IBOutlet Calculator *calculator; or something like that. In IB connect that outlet to your Calculator object. You will then be able to reference it in your ScientificCalculatorViewController.

  2. In the - (void) viewDidLoad {} method, programmatically allocate and initialise a Calculator and set it to a property in your ScientificCalculatorViewController. In this case, you can remove the object from your NIB.

Kenny
How do you refer to the ScientificCalculatorViewController from within the Calculator object? (I know according to MVC principles you are not supposed to, but I am curious)
cannyboy
Sorry for being so long in getting back, trying to tidy up a beta for a client... You would have to tell the Calculator object about it. Create a property within Calculator that is a ScientificCalculatorViewController * and set it from ScientifichCalculatorViewController in - (void) viewDidLoad {}You could set this relationship up in IB as well, just make the property an IBOutlet and do the connecting.
Kenny