Comments
yourfanat wrote: I am using another tool for Oracle developers - dbForge Studio for Oracle. This IDE has lots of usefull features, among them: oracle designer, code competion and formatter, query builder, debugger, profiler, erxport/import, reports and many others. The latest version supports Oracle 12C. More information here.
Cloud Expo on Google News
SYS-CON.TV
Cloud Expo & Virtualization 2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Cloud Computing & Enterprise IT: Cost & Operational Benefits
How and Why is a Flexible IT Infrastructure the Key To the Future?
Click For 2008 West
Event Webcasts
Leopard Code Sample: A Bound NSCollectionView
Leopard introduces a bunch of amazingly powerful new controls, but one of my favorite new controls is the NSCollectionView

Leopard introduces a bunch of amazingly powerful new controls, but one of my favorite new controls is the NSCollectionView. This control works a lot like the FlowLayoutPanel if you're familiar with Windows Presentation Foundation (WPF). It essentially is a layout container responsible for laying out a collection of subviews. You can either manually create the subview collection, or you can set the content array of the NSCollectionView. This is a really powerful option because if you can set the content array, you can also bind it. For this demo, I've bound the content array of the NSCollectionView to an array controller. If you follow along (or if you cheat and just download the code), you'll notice that the NSCollectionView subviews automatically request Core Animation layers. This means that, by default, new items fade in as they are added, but you can change that transition using the animations tab of the inspector.

To get started, just create a new Cocoa application. The first thing you do after creating the new Cocoa application is edit the project settings and set Garbage Collection to Required. Why you ask? Well, as a .NET developer, I have taken a blood oath to never write unmanaged code. If I violate this blood oath, I'm fairly certain that it will rip a hole in the fabric of space-time from which none of us will escape. So yeah, make sure GC is Required. The Cocoa application we're building is called MonsterCompendium. Rather than doing some contrived Hello World app, this app forms the basis of an app that a pen-and-paper DM might use to keep track of monsters. Obviously it's ridiculously oversimplified, but you get the idea.

I'm going to assume that you've done some Cocoa programming before (mostly because I don't have enough space to write the tutorial in a single blog post... maybe I should do a screencast?). Create yourself a new class called AppController and give it a method called newMonster and then make sure that you've got a "plus" button on your main window that invokes newMonster. Create a new class called KHMonster (feel free to change the initials so they match your name... or keep my initials if you really like them...). The source code to KHMonster.h and KHMonster.m is listed below. Take special note of the new property syntax and the fact that I've not written any retain/release code:

 #import <Cocoa/Cocoa.h>

@interface KHMonster : NSObject 

{

    NSString *trueName;

int armorClass;

int attackRating;

}

 

@property(copy, readwrite) NSString *trueName;

@property int armorClass;

@property int attackRating;

 

@end

And here's the implementation (.m) code:

 #import "KHMonster.h"

@implementation KHMonster

 

@synthesize trueName;

@synthesize armorClass;

@synthesize attackRating;

@end

With that out of the way, you can drag a new NSCollectionView onto your main window. Once you do this, you should notice that you get some extra goodies in your MainMenu.nib tray: an instance called Collection View Item and an NSView that represents the template for a single item within the collection. When you double click the NSView, define an interface that will be bound to a single instance of KHMonster. To do that, you need to set the value of your text fields to be bound to the Collection View Item (this is a special instance that acts like a proxy) with the model root path of : representedObject.trueName. representedObject is a property that points to the object being represented by that particular instance of the collection view item and trueName is a property on the KHMonster class.

In case you're curious, here's the code to the newMonster: method on the AppController class:

#import "AppController.h"

#import "KHMonster.h"

 

@implementation AppController

 

- (IBAction)newMonster:(id)sender

{

KHMonster *newMonster = [[KHMonster alloc] init];

newMonster.trueName = [NSString stringWithString:@"New Monster"];

newMonster.attackRating = 12;

newMonster.armorClass = 30;

[monsterArray addObject:newMonster];

}

@end 

Note that I've got an array controller that is serving up instances of the KHMonster class. My AppController instance has an IBOutlet called monsterArray that I've rigged up through Interface Builder to allow the app controller to refer to the array controller as in the preceding code.

Finally, after all the code is together and all the bindings are rigged up, I can run the application. In the screenshot below, you can see me editing a borderless bold text field that contains the monster's name (bound to representedObject.trueName) and a couple labels and some text fields with number formatters in them to allow them to be bound to integers properly. 

Monster Compendium Demo - Modifying newly added monster rows

And in the next screenshot you see the collection view after editing a couple more items. The reason this is important is to show you that I'm not editing the same thing, but that I am actually working with individual elements within an array. 

Monster Compendium Demo - Modified Rows

If you're familiar with Cocoa, then you know that this sample is only a few minutes worth of work away from being able to run against a local Core Data data source instead of the manual array controller of KHMonster objects.

I highly recommend you download the full sample and then go through the blog post again to see how the sample was created. Once you get the hang of using the NSCollectionView, you'll start to wonder how you ever lived without it. The keys to remember are:

  • If you are going to turn on Garbage Collection, you have to do it for every build config such as Debug and Release. The change will not automatically propagate from your debug config to your release config.
  • Remember representedObject. It is your friend. It's better than your friend, except it doesn't buy you beer. Since your friend probably doesn't buy you beer anyway, representedObject is definitely better than your friend.
Here's a link to the code download:
monstercompendium2.zip

tags:            
links: digg this  del.icio.us  technorati  reddit

About Kevin Hoffman
Kevin Hoffman, editor-in-chief of SYS-CON's iPhone Developer's Journal, has been programming since he was 10 and has written everything from DOS shareware to n-tier, enterprise web applications in VB, C++, Delphi, and C. Hoffman is coauthor of Professional .NET Framework (Wrox Press) and co-author with Robert Foster of Microsoft SharePoint 2007 Development Unleashed. He authors The .NET Addict's Blog at .NET Developer's Journal.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Leopard introduces a bunch of amazingly powerful new controls, but one of my favorite new controls is the NSCollectionView. This control works a lot like the FlowLayoutPanel if you're familiar with Windows Presentation Foundation (WPF). It essentially is a layout container responsible for laying out a collection of subviews. You can either manually create the subview collection, or you can set the content array of the NSCollectionView. This is a really powerful option because if you can set the content array, you can also bind it. For this demo, I've bound the content array of the NSCollectionView to an array controller. If you follow along (or if you cheat and just download the code), you'll notice that the NSCollectionView subviews automatically request Core Animation layers. This means that, by default, new items fade in as they are added, but you can change that transition using the animations tab of the inspector.


Your Feedback
iPhone News Desk wrote: Leopard introduces a bunch of amazingly powerful new controls, but one of my favorite new controls is the NSCollectionView. This control works a lot like the FlowLayoutPanel if you're familiar with Windows Presentation Foundation (WPF). It essentially is a layout container responsible for laying out a collection of subviews. You can either manually create the subview collection, or you can set the content array of the NSCollectionView. This is a really powerful option because if you can set the content array, you can also bind it. For this demo, I've bound the content array of the NSCollectionView to an array controller. If you follow along (or if you cheat and just download the code), you'll notice that the NSCollectionView subviews automatically request Core Animation layers. This means that, by default, new items fade in as they are added, but you can change that transition using the...
Latest Cloud Developer Stories
Sanjeev Sharma Joins November 11-13, 2018 @DevOpsSummit at @CloudEXPO New York Faculty. Sanjeev Sharma is an internationally known DevOps and Cloud Transformation thought leader, technology executive, and author. Sanjeev's industry experience includes tenures as CTO, Technical Sa...
DXWorldEXPO LLC announced today that Kevin Jackson joined the faculty of CloudEXPO's "10-Year Anniversary Event" which will take place on November 11-13, 2018 in New York City. Kevin L. Jackson is a globally recognized cloud computing expert and Founder/Author of the award win...
When applications are hosted on servers, they produce immense quantities of logging data. Quality engineers should verify that apps are producing log data that is existent, correct, consumable, and complete. Otherwise, apps in production are not easily monitored, have issues that...
The best way to leverage your Cloud Expo presence as a sponsor and exhibitor is to plan your news announcements around our events. The press covering Cloud Expo and @ThingsExpo will have access to these releases and will amplify your news announcements. More than two dozen Cloud ...
When building large, cloud-based applications that operate at a high scale, it's important to maintain a high availability and resilience to failures. In order to do that, you must be tolerant of failures, even in light of failures in other areas of your application. "Fly two mis...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021



SYS-CON Featured Whitepapers
ADS BY GOOGLE