PDA

View Full Version : Class communication in objective - C




TheReef
Jul 12, 2009, 06:14 AM
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.



SRossi
Jul 12, 2009, 06:40 AM
In the header file (.h file) you would put something like:

- (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:

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

Should work.

Stephen

TheReef
Jul 12, 2009, 07:00 AM
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:

NSRect bounds = [self bounds];

How does it know to change the NSView class?

SRossi
Jul 12, 2009, 07:14 AM
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:

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

TheReef
Jul 12, 2009, 07:25 AM
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.

lee1210
Jul 12, 2009, 09:33 AM
So just have instance vars in each that point to the other, then send messages directly.

-Lee

NickK1066
Jul 14, 2009, 04:23 AM
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.

TheReef
Jul 19, 2009, 09:06 PM
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:

[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?

Cinder6
Jul 19, 2009, 10:02 PM
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.

TheReef
Jul 20, 2009, 12:18 AM
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!