Professional Web Applications Themes

Drawing in a subclass of NSView - Mac Programming

I've created my own subclass of NSView -- call it myView. In it, I create an NSMatrix that I sometimes update. When the update is necessary, as signalled by a button click reported to another class, I send [myView setNeedsDisplay:YES] to my subclass, expecting drawRect: to be called. But it isn't. I've found that if I send [[myView superview] setNeedsDisplay:YES], everything works as I'd expect - myView's drawRect: is called, and the NSMatrix gets updated. But it bothers me: it seems to me that I shouldn't need to interact with the superview. Am I confused, or have I left something ...

  1. #1

    Default Drawing in a subclass of NSView

    I've created my own subclass of NSView -- call it myView. In it, I
    create an NSMatrix that I sometimes update. When the update is
    necessary, as signalled by a button click reported to another class, I
    send [myView setNeedsDisplay:YES] to my subclass, expecting drawRect:
    to be called.

    But it isn't.

    I've found that if I send [[myView superview] setNeedsDisplay:YES],
    everything works as I'd expect - myView's drawRect: is called, and the
    NSMatrix gets updated.

    But it bothers me: it seems to me that I shouldn't need to interact
    with the superview. Am I confused, or have I left something out of
    myView?

    Thanks,
    Steve
    Steve Ketcham Guest

  2. #2

    Default Re: Drawing in a subclass of NSView

    In <200820031201042008%sgketchamcomcast.net> Steve Ketcham wrote:
    > I've created my own subclass of NSView -- call it myView. In it, I
    > create an NSMatrix that I sometimes update. When the update is
    > necessary, as signalled by a button click reported to another class, I
    > send [myView setNeedsDisplay:YES] to my subclass, expecting drawRect:
    > to be called.
    >
    > But it isn't.
    >
    > I've found that if I send [[myView superview] setNeedsDisplay:YES],
    > everything works as I'd expect - myView's drawRect: is called, and the
    > NSMatrix gets updated.
    >
    > But it bothers me: it seems to me that I shouldn't need to interact
    > with the superview. Am I confused, or have I left something out of
    > myView?
    Neither. It's just that setNeedsDisplay doesn't work very well. m.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

  3. #3

    Default Re: Drawing in a subclass of NSView

    In article <20030820101200567-0700news.la.sbcglobal.net>, matt neuburg
    <matttidbits.com> wrote:
    > In <200820031201042008%sgketchamcomcast.net> Steve Ketcham wrote:
    > > I've created my own subclass of NSView -- call it myView. In it, I
    > > create an NSMatrix that I sometimes update. When the update is
    > > necessary, as signalled by a button click reported to another class, I
    > > send [myView setNeedsDisplay:YES] to my subclass, expecting drawRect:
    > > to be called.
    > >
    > > But it isn't.
    > >
    > > I've found that if I send [[myView superview] setNeedsDisplay:YES],
    > > everything works as I'd expect - myView's drawRect: is called, and the
    > > NSMatrix gets updated.
    > >
    > > But it bothers me: it seems to me that I shouldn't need to interact
    > > with the superview. Am I confused, or have I left something out of
    > > myView?
    >
    > Neither. It's just that setNeedsDisplay doesn't work very well. m.
    What I've done is to implement setNeedsDisplay: in myView:

    - (void) setNeedsDisplay:(BOOL) display
    {
    [[self superview] setNeedsDisplay:display];
    }

    That way, the rest of the code looks "normal" and the hack is confined
    to one place.

    Thanks for the reassurance.

    S
    Steve Ketcham Guest

  4. #4

    Default Re: Drawing in a subclass of NSView

    In <200820031319465309%sgketchamcomcast.net> Steve Ketcham wrote:
    > In article <20030820101200567-0700news.la.sbcglobal.net>, matt
    > neuburg <matttidbits.com> wrote:
    >
    >> In <200820031201042008%sgketchamcomcast.net> Steve Ketcham wrote:
    >> > I've created my own subclass of NSView -- call it myView. In it, I
    >> > create an NSMatrix that I sometimes update. When the update is
    >> > necessary, as signalled by a button click reported to another class,
    >> > I send [myView setNeedsDisplay:YES] to my subclass, expecting
    >> > drawRect: to be called. But it isn't. I've found that if I send [[
    >> > myView superview] setNeedsDisplay:YES], everything works as I'd
    >> > expect - myView's drawRect: is called, and the NSMatrix gets
    >> > updated. But it bothers me: it seems to me that I shouldn't need
    >> > to interact with the superview. Am I confused, or have I left
    >> > something out of myView?
    >>
    >> Neither. It's just that setNeedsDisplay doesn't work very well. m.
    >
    > What I've done is to implement setNeedsDisplay: in myView:
    >
    > - (void) setNeedsDisplay:(BOOL) display
    > {
    > [[self superview] setNeedsDisplay:display];
    >}
    That's clever. I usually just say display instead of setNeedsDisplay (
    also known as brute force). m.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

  5. #5

    Default Re: Drawing in a subclass of NSView

    matt neuburg wrote:
    >
    > In <200820031201042008%sgketchamcomcast.net> Steve Ketcham wrote:
    > > I've created my own subclass of NSView -- call it myView. In it, I
    > > create an NSMatrix that I sometimes update. When the update is
    > > necessary, as signalled by a button click reported to another class, I
    > > send [myView setNeedsDisplay:YES] to my subclass, expecting drawRect:
    > > to be called.
    > >
    > > But it isn't.
    > >
    > > I've found that if I send [[myView superview] setNeedsDisplay:YES],
    > > everything works as I'd expect - myView's drawRect: is called, and the
    > > NSMatrix gets updated.
    > >
    > > But it bothers me: it seems to me that I shouldn't need to interact
    > > with the superview. Am I confused, or have I left something out of
    > > myView?
    >
    > Neither. It's just that setNeedsDisplay doesn't work very well. m.
    -setNeedsDisplay: works exactly as it's doented. What trouble have
    you had with it?

    -jcr
    John C. Randolph Guest

  6. #6

    Default Re: Drawing in a subclass of NSView

    > If I send -setNeedsDisplay: to the NSTabView, the matrix is
    > redisplayed. If I send it to myView, it isn't. How does the NSTabView
    > send the message to the NSMatrix?
    It sends the message to each of its subviews.
    > Are you telling me that I should implement -setNeedsDisplay: in myView
    > to pass the message to the NSMatrix? (I can't send it directly from
    > the controller class: that class knows about myView, but not its
    > components.)
    What about [myView subviews]? Are you sure the NSMatrix is registered as
    a subview?
    >If so, that isn't what the doentation says. It says
    > that a -setNeedsDisplay message sent to an NSView causes its entire
    > bounds to be redisplayed. Since its "entire bounds" contains its
    > subviews, shouldn't they get redrawn automatically, too?
    Yes.
    > Moreover, I'm not even sure it would work. myView's -drawRect:
    > changes the cells in the matrix, so redrawing the existing matrix
    > wouldn't help.
    The -drawRect: method is not a place to be changing cells in the matrix.
    The job of -drawRect: is to make the display on the screen catch up to
    the internal state, not to create a moving target by making further
    changes to the internal state. The place to make changes is in your
    controller class, which should then call -setNeedsDisplay: as needed.
    > The question really comes down to this: why doesn't [myView
    > setNeedsDisplay:YES] result in a call to myView's drawRect:?
    Are you sure it doesn't? Have you checked it with the debugger? Maybe
    it's being called, but it isn't doing what you expected.

    --
    Dave Seaman
    Judge Yohn's mistakes revealed in Mumia Abu-Jamal ruling.
    <http://www.commoncouragepress.com/index.cfm?action=book&bookid=228>
    Dave Seaman Guest

  7. #7

    Default Re: Drawing in a subclass of NSView

    In article <bi2o8u$66q$1mozo.cc.purdue.edu>, Dave Seaman
    <dseamanno.such.host> wrote:
    > > If I send -setNeedsDisplay: to the NSTabView, the matrix is
    > > redisplayed. If I send it to myView, it isn't. How does the NSTabView
    > > send the message to the NSMatrix?
    >
    > It sends the message to each of its subviews.
    OK.
    >
    > > Are you telling me that I should implement -setNeedsDisplay: in myView
    > > to pass the message to the NSMatrix? (I can't send it directly from
    > > the controller class: that class knows about myView, but not its
    > > components.)
    >
    > What about [myView subviews]? Are you sure the NSMatrix is registered as
    > a subview?
    Certainly I'm sure. I verified it by:

    NSView *temp = [self superview]l
    do {
    NSLog ([temp description];
    temp = [temp superview];
    } while (temp != [window contentView];
    >
    > >If so, that isn't what the doentation says. It says
    > > that a -setNeedsDisplay message sent to an NSView causes its entire
    > > bounds to be redisplayed. Since its "entire bounds" contains its
    > > subviews, shouldn't they get redrawn automatically, too?
    >
    > Yes.
    But they aren't!!
    >
    > > Moreover, I'm not even sure it would work. myView's -drawRect:
    > > changes the cells in the matrix, so redrawing the existing matrix
    > > wouldn't help.
    >
    > The -drawRect: method is not a place to be changing cells in the matrix.
    > The job of -drawRect: is to make the display on the screen catch up to
    > the internal state, not to create a moving target by making further
    > changes to the internal state. The place to make changes is in your
    > controller class, which should then call -setNeedsDisplay: as needed.
    >
    Point taken -- actually, I knew that. The program morphed into this
    form as I was trying to figure out what was going on.
    > > The question really comes down to this: why doesn't [myView
    > > setNeedsDisplay:YES] result in a call to myView's drawRect:?
    >
    > Are you sure it doesn't? Have you checked it with the debugger? Maybe
    > it's being called, but it isn't doing what you expected.
    I'm as certain as I can be. It doesn't stop in the debugger.
    Moreover, since I've seen (other) debuggers actually change the
    behavior of programs, I put in an NSLog call. It never prints.

    So the question is the same: The question really comes down to this:
    why doesn't [myView setNeedsDisplay:YES] result in a call to myView's
    drawRect:?

    Steve
    Steve Ketcham Guest

  8. #8

    Default Re: Drawing in a subclass of NSView

    Steve Ketcham wrote:
    >
    > In article <3F447179.81983E21nospam.idiom.com>, John C. Randolph
    > <jcrnospam.idiom.com> wrote:
    >
    > > Steve Ketcham wrote:
    > > >
    > > > I've created my own subclass of NSView -- call it myView. In it, I
    > > > create an NSMatrix that I sometimes update. When the update is
    > > > necessary, as signalled by a button click reported to another class, I
    > > > send [myView setNeedsDisplay:YES] to my subclass, expecting drawRect:
    > > > to be called.
    > > >
    > > > But it isn't.
    > > >
    > > > I've found that if I send [[myView superview] setNeedsDisplay:YES],
    > > > everything works as I'd expect - myView's drawRect: is called, and the
    > > > NSMatrix gets updated.
    > > >
    > > > But it bothers me: it seems to me that I shouldn't need to interact
    > > > with the superview. Am I confused, or have I left something out of
    > > > myView?
    > >
    > > It sounds like you're never sending -setNeedsDisplay: to the NSMatrix.
    > >
    > > -jcr
    >
    > John,
    >
    > Here's the view hierarchy:
    >
    > NSTabView
    > |
    > myView
    > |
    > NSMatrix
    >
    > If I send -setNeedsDisplay: to the NSTabView, the matrix is
    > redisplayed. If I send it to myView, it isn't. How does the NSTabView
    > send the message to the NSMatrix?
    >
    > Are you telling me that I should implement -setNeedsDisplay: in myView
    > to pass the message to the NSMatrix? (I can't send it directly from
    > the controller class: that class knows about myView, but not its
    > components.) If so, that isn't what the doentation says. It says
    > that a -setNeedsDisplay message sent to an NSView causes its entire
    > bounds to be redisplayed. Since its "entire bounds" contains its
    > subviews, shouldn't they get redrawn automatically, too?
    >
    > Moreover, I'm not even sure it would work. myView's -drawRect:
    > changes the cells in the matrix, so redrawing the existing matrix
    > wouldn't help.
    >
    > The question really comes down to this: why doesn't [myView
    > setNeedsDisplay:YES] result in a call to myView's drawRect:?
    >
    > From the description of NSView's -setNeedsDisplay: at
    > [url]http://developer.apple.com/doentation/Cocoa/Reference/ApplicationKit/[/url]
    > ObjC_classic/Classes/NSView.html#//apple_ref/doc/uid/20000014/BBCCADHC
    >
    > "If flag is YES , marks the receiver1s entire bounds as needing
    > display; if flag is NO , marks it as not needing display. Whenever the
    > data or state used for drawing a view object changes, the view should
    > be sent a setNeedsDisplay: message. NSViews marked as needing display
    > are automatically redisplayed on each pass through the application1s
    > event loop."
    >
    > My NSView subclass isn't being redrawn, because drawRect: is never
    > called.
    >
    > I guess I'm still confused.
    So am I. Let's see the code.

    -jcr
    John C. Randolph Guest

  9. #9

    Default Re: Drawing in a subclass of NSView

    On Thu, 21 Aug 2003 12:56:06 -0400, Steve Ketcham wrote:
    > In article <bi2o8u$66q$1mozo.cc.purdue.edu>, Dave Seaman
    ><dseamanno.such.host> wrote:
    >> > The question really comes down to this: why doesn't [myView
    >> > setNeedsDisplay:YES] result in a call to myView's drawRect:?
    I have never seen a case where it didn't work as doented. Perhaps
    there is something wrong elsewhere in your program.
    >> Are you sure it doesn't? Have you checked it with the debugger? Maybe
    >> it's being called, but it isn't doing what you expected.
    > I'm as certain as I can be. It doesn't stop in the debugger.
    > Moreover, since I've seen (other) debuggers actually change the
    > behavior of programs, I put in an NSLog call. It never prints.
    > So the question is the same: The question really comes down to this:
    > why doesn't [myView setNeedsDisplay:YES] result in a call to myView's
    > drawRect:?
    Sometimes things like this can result from a program doing things with
    invalid pointers or the like.


    --
    Dave Seaman
    Judge Yohn's mistakes revealed in Mumia Abu-Jamal ruling.
    <http://www.commoncouragepress.com/index.cfm?action=book&bookid=228>
    Dave Seaman Guest

  10. #10

    Default Re: Drawing in a subclass of NSView

    In article <bi34tj$gni$2mozo.cc.purdue.edu>, Dave Seaman
    <dseamanno.such.host> wrote:
    > On Thu, 21 Aug 2003 12:56:06 -0400, Steve Ketcham wrote:
    > > In article <bi2o8u$66q$1mozo.cc.purdue.edu>, Dave Seaman
    > ><dseamanno.such.host> wrote:
    >
    > >> > The question really comes down to this: why doesn't [myView
    > >> > setNeedsDisplay:YES] result in a call to myView's drawRect:?
    >
    > I have never seen a case where it didn't work as doented. Perhaps
    > there is something wrong elsewhere in your program.
    >
    > >> Are you sure it doesn't? Have you checked it with the debugger? Maybe
    > >> it's being called, but it isn't doing what you expected.
    >
    > > I'm as certain as I can be. It doesn't stop in the debugger.
    > > Moreover, since I've seen (other) debuggers actually change the
    > > behavior of programs, I put in an NSLog call. It never prints.
    >
    > > So the question is the same: The question really comes down to this:
    > > why doesn't [myView setNeedsDisplay:YES] result in a call to myView's
    > > drawRect:?
    >
    > Sometimes things like this can result from a program doing things with
    > invalid pointers or the like.
    Let me put together a test case for you to look at.


    s
    Steve Ketcham Guest

  11. #11

    Default Re: Drawing in a subclass of NSView

    In article <210820031512187822%sgketchamcomcast.net>, Steve Ketcham
    <sgketchamcomcast.net> wrote:
    > In article <bi34tj$gni$2mozo.cc.purdue.edu>, Dave Seaman
    > <dseamanno.such.host> wrote:
    >
    > > On Thu, 21 Aug 2003 12:56:06 -0400, Steve Ketcham wrote:
    > > > In article <bi2o8u$66q$1mozo.cc.purdue.edu>, Dave Seaman
    > > ><dseamanno.such.host> wrote:
    > >
    > > >> > The question really comes down to this: why doesn't [myView
    > > >> > setNeedsDisplay:YES] result in a call to myView's drawRect:?
    > >
    > > I have never seen a case where it didn't work as doented. Perhaps
    > > there is something wrong elsewhere in your program.
    > >
    > > >> Are you sure it doesn't? Have you checked it with the debugger? Maybe
    > > >> it's being called, but it isn't doing what you expected.
    > >
    > > > I'm as certain as I can be. It doesn't stop in the debugger.
    > > > Moreover, since I've seen (other) debuggers actually change the
    > > > behavior of programs, I put in an NSLog call. It never prints.
    > >
    > > > So the question is the same: The question really comes down to this:
    > > > why doesn't [myView setNeedsDisplay:YES] result in a call to myView's
    > > > drawRect:?
    > >
    > > Sometimes things like this can result from a program doing things with
    > > invalid pointers or the like.
    >
    > Let me put together a test case for you to look at.
    >
    I found the problem!

    In IB, the class type for the myView IBOutlet was set incorrectly.
    Therefore, the connection never worked correctly.

    S
    >
    > s
    Steve Ketcham Guest

Similar Threads

  1. #40223 [NEW]: ReflectionMethod is not an subclass of ReflectionFunction
    By baldurien at bbnwn dot eu in forum PHP Bugs
    Replies: 0
    Last Post: January 24th, 07:39 PM
  2. Drawing in Flex? Drawing and AS for custom compon
    By yoway in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: April 18th, 10:19 PM
  3. delegate or subclass NSTextView?
    By None in forum Mac Programming
    Replies: 3
    Last Post: October 1st, 08:58 AM
  4. [NSView scrollRect:by:]
    By matt neuburg in forum Mac Programming
    Replies: 1
    Last Post: August 19th, 05:01 PM
  5. keyDown: in NSMatrix subclass
    By matt neuburg in forum Mac Programming
    Replies: 3
    Last Post: July 16th, 12:05 AM

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