Professional Web Applications Themes

Problem redrawing a subclassed NSView - Mac Programming

I'm trying to write a small app as part of the learning process. So far, this app consists of a controller class (myController) and a subclassed NSView (myView). When the app loads, I use the initWithFrame to populate an array of [self bounds] size with random on/off values. Then in the -draw method, I draw these to the view with bezierPath fillRect, I get a kind of random starfield effect in the view. In the viewDidEndLiveResize method, I recalculate the array and set setNeedsDisplay. This is great - if I resize the app, I get a new and bigger starfield. ...

  1. #1

    Default Problem redrawing a subclassed NSView

    I'm trying to write a small app as part of the learning process.

    So far, this app consists of a controller class (myController) and a
    subclassed NSView (myView).

    When the app loads, I use the initWithFrame to populate an array of
    [self bounds] size with random on/off values. Then in the -draw method,
    I draw these to the view with bezierPath fillRect, I get a kind of
    random starfield effect in the view.

    In the viewDidEndLiveResize method, I recalculate the array and set
    setNeedsDisplay. This is great - if I resize the app, I get a new and
    bigger starfield. Joy.

    The problem I face is trying to generate a new random starfield, I have
    a button in my controller that, when pressed, calls a method in the
    view. I've used NSLog in the view method to print a log message when
    this method is called, so I no it's going in there. I call the same
    methods as in viewDidEndLiveReszie to regenerate a new random starfield
    and set setNeedsDisplay but nothing happens - the view does not change.
    I guess because the myView draw method never gets called.

    I've tried calling [self display] from the view class and from the app
    controller, but nothing I've tried so far will make the view redraw on
    pressing this button. Only on a resize do I get a new starfield (and I'm
    gutted because I thought the resize bit would be the hard thing - most
    frustrating).

    I must be missing something obvious but what?


    --
    Remove stars for email
    g*a*r*y**k*l*i*n*g*o*n*.*o*r*g*.*u*k*
    Gary Guest

  2. #2

    Default Re: Problem redrawing a subclassed NSView

    Gary <0.0.1> wrote:
     

    The NSView method that draws its contents is not -draw, but -drawRect:.
     

    This should work. You're doing the right thing. setNeedsDisplay should
    trigger -drawRect after the call chain ends. For example, look at the
    DotView example on your hard disk, which does almost exactly what you
    describe above, and works.

    Maybe you have logic you're not telling us about that is resisting the
    change somehow. Try comparing what you are doing with what DotView does.
    There must be a significant difference somewhere! Maybe there is a flaw
    in how you "regenerate a new random starfield" and you *are* redrawing
    but getting exactly the same drawing. Stick NSLog() calls everywhere so
    you can trace your logic easily. m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  3. #3

    Default Re: Problem redrawing a subclassed NSView

    matt neuburg wrote:
     
     

    Yes, my typo in the usenet post. Sorry.
     

     

    I have NSLog in the code which generates new stars, and I have one in
    the drawRect. The generate bit does its job, and I dup out the arrays.
    drawRect never gets called.

    I will check out the DotView. Thanks.


    --
    Remove stars for email
    g*a*r*y**k*l*i*n*g*o*n*.*o*r*g*.*u*k*
    Gary Guest

  4. #4

    Default Re: Problem redrawing a subclassed NSView

    Gary wrote:

     

    Okey, following the example you gave, I added a mouseup event to my
    view, and told it to do the same thing as my appcontroller button call.
    Thus I end up with the following code:

    - (void)drawRandomPattern
    {
    NSLog("drawRandomPressed");
    [self initArray];
    [self setNeedsDisplay:YES];
    }


    - (void)mouseUp:(NSEvent *)event
    {
    [self initArray];
    [self setNeedsDisplay:YES];
    }


    The mouseUp method works fine. Clicking in the view gives a new random
    field which is displayed.

    If I click the button in the controller, I get the drawRandomPressed
    message, the array gets regenerated but nothing is drawn.

    Once more I find myself at a loss :-(



    --
    Remove stars for email
    g*a*r*y**k*l*i*n*g*o*n*.*o*r*g*.*u*k*
    Gary Guest

  5. #5

    Default Re: Problem redrawing a subclassed NSView

    Gary <0.0.1> wrote:
     
    >
    > Okey, following the example you gave, I added a mouseup event to my
    > view, and told it to do the same thing as my appcontroller button call.
    > Thus I end up with the following code:
    >
    > - (void)drawRandomPattern
    > {
    > NSLog("drawRandomPressed");
    > [self initArray];
    > [self setNeedsDisplay:YES];
    > }
    >
    >
    > - (void)mouseUp:(NSEvent *)event
    > {
    > [self initArray];
    > [self setNeedsDisplay:YES];
    > }
    >
    >
    > The mouseUp method works fine. Clicking in the view gives a new random
    > field which is displayed.
    >
    > If I click the button in the controller, I get the drawRandomPressed
    > message, the array gets regenerated but nothing is drawn.[/ref]

    Well, I'd have to suggest that perhaps the instance of your NSView
    subclass to which you are sending the drawRandomPattern message is not
    the same as the instance that's visible in your window (and which you
    click to get a mouseUp event). You do not say anything about how the
    button-in-the-controller's action method gets its reference to this
    instance, but that might be something to think about. m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  6. #6

    Default Re: Problem redrawing a subclassed NSView

    On Sun, 4 Jul 2004, Gary wrote:
     

    Your outlet is somehow ed up. It's probably not being set, either
    because you never connected it in IB, you have a method named
    setNameOfMyOutlet: that doesn't do what it says, or some other reason.
    Log/debug the actual value of the outlet.
    Michael Guest

  7. #7

    Default Re: Problem redrawing a subclassed NSView

    Michael Ash wrote:

     

    I found it :-)

    The button in my contoller class was calling a method in the view that
    was not an IBAction method. I changed this around and did the ctrl-drag
    thing in IB from the button to the view and connected the action and now
    the button in the controller class redraws the view as desired.

    I'm finding the whole IB/outlet/action thing much more mind bending than
    the ObjC!

    Getting there though. Thanks for everyones help.

    --
    Remove stars for email
    g*a*r*y**k*l*i*n*g*o*n*.*o*r*g*.*u*k*
    Gary Guest

Similar Threads

  1. Replies: 0
    Last Post: February 7th, 03:00 PM
  2. How to intercept property changes in subclassed control?
    By jonwillis in forum Macromedia Flex General Discussion
    Replies: 1
    Last Post: May 8th, 03:46 PM
  3. Redrawing of Tk::Columns
    By Massi in forum PERL Modules
    Replies: 5
    Last Post: November 13th, 01:55 PM
  4. Drawing in a subclass of NSView
    By Steve Ketcham in forum Mac Programming
    Replies: 10
    Last Post: August 22nd, 12:31 AM
  5. [NSView scrollRect:by:]
    By matt neuburg in forum Mac Programming
    Replies: 1
    Last Post: August 19th, 05:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139