There is an optional method for the application's delegate which will do this automatically. All you have to do is add this to the implementation. You don't need to create an outlet or anything.
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
return YES;
}