Reputation: 767
New to Objective C, Project Builder and IB so please forgive my lack of knowledge...
I am attempting to create a simple program to learn and understand the use of the NSView class. It consists of a single window with a single NSView
and four buttons labeled, RED, GREEN, BLUE, and YELLOW.
The goal is to push a button and have the NSView fill with the appropriate color. I have attempted several variations of the code but none seem to get any closer to a result. Currently, upon execution, the window appears with a solid black square (my NSView
subclass/object called MyView) and the four buttons.
I can successfully change the initial color of MyView within the MyView.h/.m files, however, I seem to be unable to change the color or redraw the MyView from my controller code (called AppController.h/.m). I always receive a compiler warning.
The interface was created in IB and the Custom View object was pointed to the MyView subclass. I've attached my code below.
//---------------------------
//
// MyView.h
//
#import Cocoa/Cocoa.h
@interface MyView : NSView
int myColorChoice;
@end
//--------------------------
//
// MyView.m
//
#import "MyView.h"
@implementation MyView
int myColorChoice = 4;
-(void) setMyColorChoice : (int) c {
myColorChoice = c;
}
-(void) drawRect: (NSRect) rect {
switch (myColorChoice) {
case 1 :
[[NSColor greenColor] set];
break;
case 2 :
[[NSColor blueColor] set];
break;
case 3 :
[[NSColor redColor] set];
break;
case 4 :
[[NSColor yellowColor] set];
break;
}
NSRectFill([self bounds]);
}
@end
//--------------------
//
// AppController.h
//
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
IBOutlet id display;
}
- (IBAction)makeBlue:(id)sender;
- (IBAction)makeGreen:(id)sender;
- (IBAction)makeRed:(id)sender;
- (IBAction)makeYellow:(id)sender;
@end
//---------------------
//
// AppController.m
//
#import "AppController.h"
#import "MyView.h"
@implementation AppController
- (IBAction)makeGreen:(id)sender {
[MyView setMyColorChoice:1];
[MyView setNeedsDisplay:YES];
}
- (IBAction)makeBlue:(id)sender {
[MyView setMyColorChoice:2];
}
- (IBAction)makeRed:(id)sender {
[MyView setMyColorChoice:3];
}
- (IBAction)makeYellow:(id)sender {
[MyView setMyColorChoice:4];
}
@end
I'm fairly confident that i'm simply violating some basic principle, but I just can't seem to wrap my head around this one. I suspect it must be something to do with MyView being improperly set up, as if I try to call any of my methods, the compiler gives me the "MyView may not respond to..." for any of the methods I've attempted to call.
NOTE: I've only been modifying the makeGreen method for experimentation. Once that works, I'll duplicate the code for the other colors.
Any help would be greatly appreciated!
Thanks guys.
Upvotes: 2
Views: 279
Reputation: 547
One thing to consider is setting up your setMyColorChoice
method like this:
-(void)setMyColorChoice:(NSColor *)newColor
{
[newColor set];
NSRectFill([self bounds]);
[self setNeedsDisplay:YES];
}
Then you can clean up your drawRect
method instead of using integers and a switch. This would be the most efficient way since you don't need to declare instance variables thus saving memory.
Upvotes: 1
Reputation: 126107
You're attempting to call instance methods on the MyView
class. Your AppController
needs a reference to an instance of that class. Probably the best way to do that is with an IBOutlet
that you've hooked up in IB... if that's what your display
ivar is, then all you need to do is call [display setColorChoice:1]; [display setNeedsDisplay:YES];
etc.
Upvotes: 0