Setting NSTextField inside drawRect:

  • I have in 10.4.11 an NSWindow with a MyView and an NSTextField.

    - (void)drawRect:(NSRect)rect
    {
    [ [ NSColor redColor ] set ];
    NSRectFill( rect );
    [ textField setStringValue: @"Buh" ];

    // without the following line, the textField will never show its new
    text:
    [ self performSelector: @selector(textFieldUpdate)  withObject: nil
    afterDelay: 0.0 ];
    }

    - (void)textFieldUpdate { [ textField setNeedsDisplay: YES ]; }

    Why does setting an NSTextField form inside drawRect: not work?
    Is this fact documented somewhere?

    Kind regards,

    Gerriet.
  • On 2008 Feb, 16, at 0:01, Gerriet M. Denkmann wrote:

    > I have in 10.4.11 an NSWindow with a MyView and an NSTextField.
    >
    > - (void)drawRect:(NSRect)rect
    > {
    > [ [ NSColor redColor ] set ];
    > NSRectFill( rect );
    > [ textField setStringValue: @"Buh" ];
    >
    > //    without the following line, the textField will never show its
    > new text:
    > [ self performSelector: @selector(textFieldUpdate)  withObject:
    > nil  afterDelay: 0.0 ];
    > }
    >
    > - (void)textFieldUpdate { [ textField setNeedsDisplay: YES ]; }
    >
    > Why does setting an NSTextField form inside drawRect: not work?

    I can't say for sure, because you did not explain which class
    implementation the above code was copied from.  But a possible
    explanation is that your drawRect: method is not drawing your
    textField.  When you implement drawRect:, you're the man!  The system
    assumes that drawRect: is going to draw ^everything^ in the rect
    argument that it is given.
  • >
    > On 2008 Feb, 16, at 0:01, Gerriet M. Denkmann wrote:
    >
    >> I have in 10.4.11 an NSWindow with a MyView and an NSTextField.
    >>
    >> - (void)drawRect:(NSRect)rect
    >> {
    >> [ [ NSColor redColor ] set ];
    >> NSRectFill( rect );
    >> [ textField setStringValue: @"Buh" ];
    >>
    >> //    without the following line, the textField will never show its
    >> new text:
    >> [ self performSelector: @selector(textFieldUpdate)  withObject:
    >> nil  afterDelay: 0.0 ];
    >> }
    >>
    >> - (void)textFieldUpdate { [ textField setNeedsDisplay: YES ]; }
    >>
    >> Why does setting an NSTextField form inside drawRect: not work?
    >
    > I can't say for sure, because you did not explain which class
    > implementation the above code was copied from.
    Sorry about this. It is the drawRect: of MyView.
    > But a possible explanation is that your drawRect: method is not
    > drawing your
    > textField.  When you implement drawRect:, you're the man!  The system
    > assumes that drawRect: is going to draw ^everything^ in the rect
    > argument that it is given.
    The NSTextField is not inside MyView, but below it; both are in the
    same window.

    I have another app, where the setStringValue: inside drawRect: does
    trigger a display of the text field, but without erasing it - so that
    the old and the new text are both visible, one on top the other -
    looks rather messy; but this is not reproducible in my test app.

    Kind regards,

    Gerriet.
previous month february 2008 next month
MTWTFSS
        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    
Go to today