Re: Noob question: Weirdness getting the bounds of an NSView...
Re: Noob question: Weirdness getting the bounds of an NSView...
- Subject: Re: Noob question: Weirdness getting the bounds of an NSView...
- From: "Luca C." <email@hidden>
- Date: Tue, 24 Mar 2009 22:16:55 +0100
Hi Jim I'd expect the code you provided to work properly without errors.
2009/3/24 Jim Anderson <email@hidden>
> Hey, folks,
>
> I'm reasonably new to Cocoa developing. I'm playing around with a subclass
> of NSView which generates some stuff based on the size of the view. The
> code basically looks like:
>
> -----------------------------
> @interface FooView : NSView {
> NSRect myBounds;
> }
> - (void)doSomeStuff;
> @end
>
> @implementation FooView
> - (void)doSomeStuff
> {
> // myBounds at this point is set to {0, 0, 0.0, 0.0}, even if this is
> called after drawRect is fired
This probably happens because you haven't set myBounds, so its fields are
the same than the ones of NSZeroRect (that is to say {{0, 0}, {0.f, 0.f}}).
I suppose you call this method before the assignment done in -drawRect:.
>
>
> // This will set myBounds to {0, 0, 0.0, 0.0}
> myBounds = [self bounds];
This can only happens if you passed NSZeroRect to -initWithFrame:.
> }
>
> - (void)drawRect: (NSRect)aRect
> {
> myBounds = [self bounds];
> }
> @end
> -------------------------
>
> The weird thing is, when drawRect gets called, [self bounds] returns a
> reasonable looking bounding rectangle. When doSomeStuff gets called, [self
> bounds] returns {0, 0, 0.0, 0.0}. The extra weird thing is, if doSomeStuff
> is called *after* drawRect gets fired, myBounds is {0, 0, 0.0, 0.0},
> *despite* it being set to something reasonable in drawRect.
[self bounds] never fails, AFAICT. I think you are making confusion with
some assignments.
But tell me: why do you need an ivar for the view's bounds? NSView has a
private ivar for that and you can access it whenever and wherever you want.
Caching its value is quite useless and needs more code, as you should
update it every time your view will be resized.
--
--Luca C.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden