Sending messages in Objective C?

Started by
1 comment, last by Mitchell314 14 years, 10 months ago
How am I supposed to send messages between classes in different files? I've got a controller for intercepting buttons to change the screen's colors, in "MyColorController.m," and an associative header. I've also got a controller for an OpenGL screen in "myOGLview.m," which also has a header. My goal is to have this flow: Step 1: User presses button Step 2: MyColorController sets it's own color value according to buttons pressed Step 3: A miracle happens, and myOGLview class some how gets the new color Step 4: myOGLview draws a simple flat shaded rectangle Steps 1,2, and 4 are all accomplished, I know how to do that stuff. But I can't get step 3 to work. According to the "MyColorController" files, the other files don't exist, and vice versa. Global variables don't work. And everything I google tends to pull up stuff I already know or don't need to know for the moment.
Advertisement
Someone needs to know about the other in order to communicate.

Now, because everything depends on the button being pressed, it makes sense to have the button tell the color controller that something needs to happen.
class ColorController{public:   void ChangeColor()   {      // Do stuff here   }};class Button{public:   Button(ColorController* controller)      : colorController(controller)   {   }   void OnPress()   {      colorController->ChangeColor();   }private:   ColorController* colorController;};

However, the above code ties the Button class very closely to the ColorController class. That's not a smart idea, because you'll want to use buttons in other places, too, and those have nothing to do with color controllers.

But instead of storing a pointer or reference to a specific class and calling a specific function on it, it's possible to create a common interface for it. This is what the observer pattern is about, for example. We can create a ButtonListener class that contains a set of functions, and have ColorController inherit from it. Now, a Button can store a ButtonListener pointer (or better, a list of pointers, so multiple other objects can react to a single button-press).

Now, buttons are often placed somewhere. In a menu, or a game screen. It's often best to let that menu or screen listen for button events, and have them react to it, rather than attaching a ColorController to a button directly. After all, a button doesn't (shouldn't) know about the color controller, so it doesn't know what color to set it to either. That's the button and color controller owners responsibility. ;)
Create-ivity - a game development blog Mouseover for more information.
Thanks.

I tried it that way, but then I ran into the problem that I need to use class methods for one to communicate to the other. But I don't know how to set any variables using class methods, and I don't know where the instances (I assume the framework is creating them) are.

This topic is closed to new replies.

Advertisement