Reputation: 44
I have a window that does drawing with NSBezierPath, there is a separate window with sliders and wish to update the drawing whenever the slider has moved. Does one need a separate subclass of each window and separate objects? What is the right way to do outlets and file's owner in this scenario?
In this case I have two windows connected to one class. The window would not update drawing, but the window with the slider would be drawn onto. I do not understand why it is confusing self with sender. Sender is the slider.
- (IBAction)branchSliderChange:(id)sender {
numberofbranches = [ _branchSlider intValue ] ;
[_branchLabel setIntegerValue: numberofbranches ];
[self drawRect:self.bounds];
[self setNeedsDisplay:YES];
}
Upvotes: 0
Views: 51
Reputation: 299265
First, you never call drawRect:
, that called by Cocoa. You just indicate when you need to be drawn with setNeedsDisplay:
.
Peer views (windows) do not talk to each other. They either talk through a controller or through the model. The model are the objects that hold the data that all the views represent. Model objects are separate from the UI (views) and controllers.
For instance, if your slider modifies the current color, the slider's controller would update your model to indicate the current color. The slider (and its controller) should not care whether that causes drawing in some other view or not; that's not their business. The drawing view's controller should then observe the model change (via KVO, notifications, delegation), and update its view accordingly.
If multiple views share a controller and the information doesn't impact anything outside that controller, the change may not go all the way down to the model. The controller might just update everything it controls. But in your case, where there's another window, there are probably multiple controllers involved, so you likely need to store this in a model class.
See Model-View-Controller in the Concepts in Objective-C Programming Guide. This is one of the most important concepts in Cocoa development, so you want to make sure you understand it.
Upvotes: 1