Cocoa NSTabView coding style question

。_饼干妹妹 提交于 2020-01-01 19:39:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!