Class communication in objective - C

Discussion in 'Mac Programming' started by TheReef, Jul 12, 2009.

  1. macrumors 68000

    TheReef

    Joined:
    Sep 30, 2007
    Location:
    NSW, Australia.
    #1
    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.
     
  2. macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #2
    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
     
  3. thread starter macrumors 68000

    TheReef

    Joined:
    Sep 30, 2007
    Location:
    NSW, Australia.
    #3
    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?
     
  4. macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #4
    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
     
  5. thread starter macrumors 68000

    TheReef

    Joined:
    Sep 30, 2007
    Location:
    NSW, Australia.
    #5
    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.
     

    Attached Files:

  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    So just have instance vars in each that point to the other, then send messages directly.

    -Lee
     
  7. macrumors regular

    Joined:
    Jul 23, 2007
    #7
    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".

     
  8. thread starter macrumors 68000

    TheReef

    Joined:
    Sep 30, 2007
    Location:
    NSW, Australia.
    #8
    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?
     
  9. macrumors 6502

    Cinder6

    Joined:
    Jul 9, 2009
    #9
    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.
     
  10. thread starter macrumors 68000

    TheReef

    Joined:
    Sep 30, 2007
    Location:
    NSW, Australia.
    #10
    Thankyou, that worked great!
     

Share This Page