简体   繁体   中英

window, view and subviews creation - mac os x

I am an Objective-C/Cocoa newbie and I was wondering if you could provide me with some guidelines regarding the first Mac OS X desktop application I am writing.

The model part is the self-organizing map algorithm, SOM (Kohonen, 1982).

From the interface point of view, the idea is to have a main window that contains the 1024x768 parent view and 192 64x64 squares (child views). The colours of the child views will change as the SOM algorithm runs. After the SOM is built, users will be able to listen to sounds mapped to a certain square by clicking on it.

The SOM algorithm is already implemented but I am having a hard time to create the interface and connect it to the model part.

I thought of creating the window by using the code below:

NSRect windowRect = NSMakeRect(0.0f, 0.0f, 1024.0f, 790.0f);

NSWindow *window = [[NSWindow alloc] initWithContentRect:windowRect 
                                               styleMask:( NSResizableWindowMask |         NSClosableWindowMask | NSTitledWindowMask) 
                                                 backing:NSBackingStoreBuffered defer:NO];

[window setBackgroundColor:[NSColor blueColor]];    

[window makeKeyAndOrderFront:nil];

Then I would create the main add the main view as a subview of the content view of my window:

 NSView *view = [[NSView alloc] init]; 

 [window setContentView: view]

Finally, I would create the child views and add them as subviews of the parent view:

 for(int i=0;i<12;i++)
 {
    for(int j=0;j<16;j++)
    {
       NSView *child = [[NSView alloc]initWithFrame:CGRectMake(64*i,64*j,64, 64)];
       [view addSubview:child];
    }
 }

My questions would be:

1) Do my ideas make sense?

2) Where should those pieces of code go? I am still in doubt where they should be part of an AppDelegate or ViewController?

3) What needs to be add to the main method to load the interface? At the moment the main method only call methods that refer to audio-processing and the SOM algorithm. I guess the general question here is how would model and view parts be connected.

I apologize for the naive questions and thank you in advance for any ideas that would help me make headway.

Thank you.

Cocoa views are somewhat expensive. Apple discourages using very large numbers of them. 192 is pushing it. Also, if the views are just going to be colored rectangles, it makes little sense to use a whole view object just for that. Just define a single custom view that draws the colored squares within it and handles clicks.

Also, you don't need to allocate a standard NSView and set it as the window's content view. The window starts out with a standard NSView as the content view. You typically only set the content view if a) it's something other than a standard NSView (a subclass of some sort) or b) it has a hierarchy of subviews already set up and you want to swap that in.

Regarding your question 3, I would say you need to follow the standard design of a Cocoa application. The main() function should follow the template provided with a new Cocoa app project. It should typically just call NSApplicationMain() . Then, a good place to add your custom code would be the -applicationDidFinishLaunching: method of the app delegate. (I don't know if you conceive of your app as document-based. If so, then the design would be somewhat different. You'd define your document class and the document window and the framework would take care of opening a new one at startup.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM