Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TheReef

macrumors 68000
Original poster
Sep 30, 2007
1,888
167
NSW, Australia.
Hi all,
Firslty my Cocoa programming experience is very low.

I've been going through the Aaron Hillegass book, and looking at Chapter 17, drawing custom views, specifically at the first one that sets the view green.

They create a NSView class called StretchView and has the drawRect method.

What I want to do is have 3 buttons, where each makes the view turn red, blue or green.


So I have the NSView class, and a controller class for the buttons. How would I go about making them talk to eachother?

Thanks.
 
In the header file (.h file) you would put something like:

Code:
- (IBAction)changeRed:(id)sender

Then create a button in Interface builder and connect it that action.

Then in the implementation file (.m file) you would put something like:

Code:
- (IBAction)changeRed:(id)sender
{
     NSRect bounds = [self bounds];
     [[NSColor redColor] set];
     [NSBezierPath fillRect:bounds];
}

Should work.

Stephen
 
Hi Stephen, thanks for your reply.

I put that code in the app controller class for the button, but get the error:

"warning: 'AppController" may not respond to '-bounds'"

for the line:

Code:
NSRect bounds = [self bounds];

How does it know to change the NSView class?
 
Hi Stephen, thanks for your reply.

I put that code in the app controller class for the button, but get the error:

"warning: 'AppController" may not respond to '-bounds'"

for the line:

Code:
NSRect bounds = [self bounds];

How does it know to change the NSView class?

I've just noticed that you created a different controller for each button, this isn't needed, you can easily create buttons onto the NSView subclass. Place the code in that .h and .m and it should work.

Stephen
 
I've just noticed that you created a different controller for each button, this isn't needed, you can easily create buttons onto the NSView subclass. Place the code in that .h and .m and it should work.

Stephen

I wanted to do it by having the buttons with the app controller and the view stuff with the NSView class :), I was wondering how I'd link/call classes, it was easy with RB/VB - I'm trying to achieve calling something like the following (example in VB/RB):

myclass2.method(paramters)

By pressing a button in myclass1 for example.
 

Attachments

  • Picture 1.png
    Picture 1.png
    26.8 KB · Views: 110
Probably the reason you're having issues s that you don't call drawRect directly.

Go into Xcode > Help > Documentation

Then type "Cocoa Fundamentals Guide". In that guide go to Appendix A and within it you'll find "How Views Get Drawn" and "Displaying a View".

Rather than displaying views immediately, the recommended course for most situations is to use the auto-display mechanism and during an event cycle mark views (or parts of views) as needing display.
...
You can also mark portions of views and their subviews as needing display and then have them redrawn at once, instead of waiting for the auto-display mechanism to trigger. The NSView display methods that offer this feature all begin with displayIfNeeded.... Even though display is immediate, these methods are more efficient than sending display or displayRect: messages to isolated views.
 
Thanks guys,
Still having some problems:

1) So I wired an outlet from appcontroller to the view in the window, in the buttons action I send the view:

Code:
[myView setNeedsDisplay];

I get "unrecognised selector sent to instance" in console.

2) How do I tell the drawRect method to run different code (ie make different colours for different buttons) if I can't call it directly?
 
1. Change it to [myView setNeedsDisplay:YES];

2. If you're insisting on using drawRect, you can give the view a background color variable (maybe NSColor *backgroundColor). When you hit the button, the controller sets the backgroundColor to the appropriate color.

Then in drawRect, instead of [[NSColor redColor] set], you'd write [backgroundColor set].

Hope that helps.
 
1. Change it to [myView setNeedsDisplay:YES];

2. If you're insisting on using drawRect, you can give the view a background color variable (maybe NSColor *backgroundColor). When you hit the button, the controller sets the backgroundColor to the appropriate color.

Then in drawRect, instead of [[NSColor redColor] set], you'd write [backgroundColor set].

Hope that helps.

Thankyou, that worked great!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.