开发者

Communicating between classes set up in nib, in code?

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜