• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Experimenting with Views (previously Noob playing with Windows)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Experimenting with Views (previously Noob playing with Windows)


  • Subject: Re: Experimenting with Views (previously Noob playing with Windows)
  • From: "Louis C. Sacha" <email@hidden>
  • Date: Wed, 14 Jul 2004 20:48:19 -0700

Hello...

The main problem is that in the line

NSArray *mySubViews = [[NSArray alloc] init];

you create a new empty instance of NSArray which your mySubViews pointer points to, and then in the line

NSEnumerator *enumerator = [mySubViews objectEnumerator];

you create an enumerator from it (and since the array is empty, the enumerator doesn't contain any objects either).

What you are really trying to do is

NSArray *mySubViews = [tabView subviews];
NSEnumerator *enumerator = [mySubViews objectEnumerator];

which will result in an enumerator that contains the subviews of the tabview.


There are also a variety of other minor issues that you will probably want to address.

You leak the memory used by the array mentioned above, which you create and then replace with the array of subviews you get from the tabview, which is easy to fix since you don't need to create the array.

In Cocoa, the indexes of arrays are 0 based, which means that for an array containing 2 objects, the valid indexes are 0 and 1. The test of your for loop should limit i to values less than the count, so you don't need to add 1 to "index". Because you are adding 1 to the count in your loop, you are going through the loop twice even though there is only one subview.

If you are going to use an NSEnumerator, you would generally use a while loop based on the existence of the object returned by the enumerator when nextObject is called, and not a for loop.

Since your enumerator is created from an empty array, it contains nothing and the pointer returned from the nextObject call is always nil. Likewise, the value returned from the frame method is also nil (since you send the message to views which is nil), which is a problem since the return type was supposed to be a struct and not a pointer, but it didn't do anything too nasty in this particular case.


Also, the Foundation function NSStringFromRect(NSRect aRect) is great for logging, and saves quite a bit of typing and/or copying and pasting. The output string has the form "{{aRect.origin.x,aRect.origin.y},{aRect.size.width,aRect.size.height}}" replaced with the appropriate values.


So, putting all of this together, you would end up with something like this:


- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
NSLog(@"TAB DELEGATE tabView:didSelectTabViewItem:")

/* this gets a pointer to an array of subviews */
NSArray *mySubViews = [tabView subviews];

/* this gets an enumerator which contains the objects in the array */
NSEnumerator *enumerator = [mySubViews objectEnumerator];

NSLog(@"There are %d subviews in this view", [mySubViews count]);

NSRect frameUnionRect = NSZeroRect; /* starting with an empty rect */

/* this while loop iterates through all the objects in the enumerator */
NSView *subview = nil;
NSRect subviewFrame;
while((subview = [enumerator nextObject]))
{
subviewFrame = [subview frame];
NSLog(@"subviewFrame is %@", NSStringFromRect(subviewFrame));

frameUnionRect = NSUnionRect(frameUnionRect, subviewFrame);
NSLog(@"frameUnionRect is %@", NSStringFromRect(frameUnionRect));
}

/* ... do whatever else ... */
}


Hope that helps,

Louis
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Experimenting with Views (previously Noob playing with Windows) (From: Nicholas <email@hidden>)

  • Prev by Date: NSTable Examples
  • Next by Date: NSTableView
  • Previous by thread: Experimenting with Views (previously Noob playing with Windows)
  • Next by thread: Re: core data
  • Index(es):
    • Date
    • Thread