 |
 |
Simple NSView Question... Please help!
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: Valencia, CA
Status:
Offline
|
|
Hey there. This is so simple, but something has me stumped!
Kind of new to Cocoa, and I'm tring to do the following...
Goal
I want to draw a circle in a custom view, with parameters set from two text boxes and a colorwell. The circle is drawn whenever the user clicks a button. I subclassed NSView and dragged an instance to the window, setting the NSView subclass as its custom class (called "circleView"). I created outlets for that class so that it can get the values from the text fields and the color well. Then, I have a controller class that handles the button press.
Problem
I'm getting a warning that says the target circleView may not respond to +setNeedsDisplay.
Do I have to use something other than the custom view's class name to reference the NSView that needs updating?
Here's the controller class (which is where I'm getting the error):
/* circleController implementation file */
#import "circleController.h"
@implementation circleController
- (IBAction)draw:(id)sender
{
[circleView setNeedsDisplay:YES];
}
@end
I can post the rest of the code if it's needed. Any ideas? I would greatly appreciate any help you could offer.
Thanks
-jbk
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
If the error message really does include the "+" in +setNeedsDisplay, then it sounds like your calling that method on the class instead of on an instance of the class.
(methods definitions beginning with "+" are class methods, those beginning with "-" are instance methods. You almost always use instance methods, and setNeedsDisplay is an instance method of the superclass NSView, I think)
I see you're calling [circleView setNeedsDisplay:YES] so if circleView is a class name then this is the problem. What you need to do is instantiate the class, and then send this method to that instance.
You can instantiate the class in Interface Builder (which I think is what you want to do), create an IBOutlet in XCode and then connect the outlet to the instance in interface builder.
If you want to create an instance programatically, then you've got to alloc, and init, etc which is usually not something you want to do for NSViews.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: Valencia, CA
Status:
Offline
|
|
Thanks for your prompt reply, Brass.
My understanding was that for custom NSViews you instantiate the class by dragging a Custom View from the IB palette window onto the application window. Is that what you mean by instantiate it in IB? As you surmised, circleView is the class name in my example, so I guess that's the problem. Do I need to instantiate it another way?
Regardless, I found an example from the BNR that provides another approach to my problem. It's the OvalWindow example found here: http://www.bignerdranch.com/resources/ In that example they implement the methods associated with the NSView subclass (changeColor: and setInnerColor:) in the NSView subclass. That makes sense right? I think that makes more sense to put that stuff directly into the NSView subclass rather than create a controller class.
However, I still am curious how to send the setNeedsDisplay: selector to a NSView subclass target. In the BNR example it always calls that selector like this: [self setNeedsDisplay:YES].
Thanks for your help.
-jbk
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
Yes, you've instantiated it in IB by adding it to a window. Howewver, in your code, you are not sending a message to that object, but instead you're sending the message to a class ("circleView"). Your object is an instance of that class, but you could have several instances, so you need to send the message to that specific instance.
If it's instantiated in IB, this is usually done by connecting an IBOutlet to that object. For example, you might have something in your controller's interface file like this:
IBOutlet CircleView *theCircle;
Then in IB, either add an outlet with the same name (theCircle) to your controller class, or read (or re-read) the controller class's interface file (.h).
Then drag a connection (CONTROL-drag) from the controller instance object to the CircleView instance object, and choose to connect the "theCircle" outlet.
Then in your controllers's implementation, you can send messages to the object (instead of to it's class):
[theCircle setNeedsDisplay:YES]
I hope this makes sense.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|