问题
I have a coding style question which probably should be asked of a senior mac programmer at work - but since I'm the only mac programmer, well, SO it is. I have a pop-up GUI for my software (3D models, data visualization) and the pop-up is Mainly a Tabbed control with a ton of stuff in each tab (sliders, radio buttons, checkboxes, etc.) With something like 20 controls per tab, and maybe half a dozen tabs... using a single controller for all the views is going to get unwieldly very quickly.
Is having a MainViewController which loads a bunch of Tabs good style?
NSView *tabA = [[NSView alloc] initWithNibName:@"tabA.nib" bundle:[NSBundle bundleWithPath:@"/Applications/BOB.app"]];
NSView *tabB = [[NSView alloc] initWithNibName:@"tabB.nib" bundle:[NSBundle bundleWithPath:@"/Applications/BOB.app"]];
It's kindof how I do it on iOS, but I'm not sure for Mac OS X. I prefer a style that offers maintainability and flexibility, as the code is going through prototyping and I may need to change it frequently.
If it's not good style, what is?
Thanks!
回答1:
I think yours is a reasonable style. You create an NSViewController
subclass for each tab, and assign it to the NSTabView
using NSTabViewItem
.
By the way, I think it's better to have
NSViewController *tabAcontroller = [[TabAController alloc] init];
with @interface TabAController:NSViewController ... @end
with init
defined as
-init{
self=[super initWithNibName:@"tabA" bundle:nil];
if(self){
...
}
return self;
}
Note that you don't need the extension .nib
when you call initWithNibName:bundle:
. And you should not specify the hard-coded path of the app. In iOS, the app's position is a given by the OS (with cryptic folder names,) but on OS X a user can freely move the app bundle to anywhere he wants. So, never refer to the main bundle as [NSBundle bundleWithPath:@"hard coded path"]
. Use just [NSBundle mainBundle]
, or just nil
in most cases. It's written in the documentation when you can just use nil
.
来源:https://stackoverflow.com/questions/3343702/cocoa-nstabview-coding-style-question