Objective-C classes composition
I开发者_Go百科'm trying to clarify some of Obj-C classes composition issues. I have 3 classes: Car, Control, and Wheels.
Car class initializes Control and Wheels classes. I was going to create a simple test iPhone application containing a UISlider
and UILabel
. When UISlider value is changed, a message must be sent (I was going to send it from Control class) to one of the Wheels class methods.
So the question is: how to properly send a message from Control directly to Wheels? Should I handle Control messages and Wheels responds only through Car class methods?
The Model-View-Controller ("MVC") pattern is what is used in Cocoa/UIKit. In this pattern, your Car and Wheels seem like they're in the model, and the UISlider and UILabel in the View. I'm not sure what your "Control" class represents. You then generally have a "Controller" that coordinates between the UI and the Model-- takes UI events and turns them into changes in the model and vice-versa.
So you might have a "SettingsView" that contains your UISlider and UILabel, and a "SettingsViewController" that has some methods to receive the UISlider and UILabel settings and apply them to the Car.
精彩评论