views:

44

answers:

1

Hi, Newbie here, I am trying to send actions from button touches to a controller other than the one acting as File's Owner. I have four distinct areas of the screen that I would like managed by four separate controllers (buttonController, toolbarController, textController and graphicController) with a fifth controller (mainController) controlling the other four. mainController is also the one associated with File's Owner. To get button presses to be handled by mainController is easy:

MainController.h file:

- (IBAction)buttonIsTouched:(id)sender;

MainController.m file:

- (IBAction)buttonIsTouched:(id)sender {
    ..handle button touch event etc.
}

Then in Interface Builder, associate the button Touch Down event with File's Owner and select buttonIsTouched and away you go. All works fine. However, when I do exactly the same thing for a controller that is not the File's Owner controller the application crashes. Here is what I did:

Create four controllers as instance variables of mainController. Instantiate them in -[MainController viewDidLoad]. Provide a button handling method exactly as above In Interface Builder, drag an Object template from Library (four times) onto the mainController.xib browser. Set the type for these objects to ButtonController, ToolBarController, TextController and GraphicsController respectively. Associate the Touch Down button event with the ButtonController object and select the buttonIsTouched entry (in the little pop-up box) Build and run the application Click the button Crash - sorry, I didn't write down the error code but it was akin to INVALID_ACCESS

Of course, I can have all the input go first to mainController and then delegate down to each individual controller but this seems an inelegant solution especially since I want the lower controllers to do some work before messaging upstream to mainController. Having to go through mainController and then back kind if irks me.

If anyone knows how to do this and has some sample code that does something similar to what I want to do I would appreciate it.

ac

+1  A: 

One problem of your approach is that your instantiate two objects for each of your four sub controllers ButtonController, ToolBarController, TextController and GraphicsController. You’re creating the controllers programmatically in viewDidLoad, but they have already been instantiated from the loaded nib.

You shouldn’t create the controllers in viewDidLoad, but instead use retaining IBOutlet properties in your MainController to wire them up in IB.

Then the controller objects are owned by the MainController and are not removed after nib loading. This will also remove your memory error.

Nikolai Ruhe