Im trying to call an IBAction Method in two classes that is called whenever a button that I created in interface builder is clicked. What I really want to happen is for a NSRect to appear whenever the button is clicked but the button and the place where I want the NSRect to appear are in separate views, so the button is in View A and the destination for the rect is in View B. I have tried doing this with NSNotificationCenter but it did not work.
Share
You are missing the C in MVC. Cocoa uses the Model-View-Controller design pattern and you seem to be missing a controller.
You should create a controller class (possibly a subclass of
NSWindowControllerso that it is responsible for the window) that implements an action method such as-buttonPressed:which is connected to your button. The controller should manage the model (which in this case is whatever the rectangles represent) such that when you press the button, the model is updated. The controller should then make your rectangle view redraw itself.Your rectangle view should be set up so that it implements a datasource pattern (see the
NSTableViewdatasource implementation for a good example) so that it knows how many and where to draw the rectangles. If you set your controller as the view’s datasource, your view doesn’t need to know anything about the model.Your rectangle view should be set up something like this:
RectangleView.h:
@protocol RectangleViewDataSource;
RectangleView.m:
You would assign your controller as the datasource of the view and make it implement the
RectangleViewDataSourceprotocol methods.The controller would look something like this:
YourController.h:
YourController.m: