How to manage multiple windows in Cocoa applications using Interface Builder

I have this application with 3 classes: AppController, Profile, ProfileBuilder. I also need 3 windows: one for each class. I tried to save all 3 as subclasses of NSObject and apply initWithNibName to the WindowController variable of the NSWindowController class, but when I tried to output some values ​​to each window, this did not work, and the window turned out as null using NSLog. I was wondering how best to manage multiple windows, possibly all from the same class as the AppWindowsController with at least some code in other classes, and, if possible, other classes as subclasses of NSObject, not NSWindowController, Therefore, if there is, perhaps, a way to remotely control the behavior of windows by adding at least code within certain classes in order to focus them most clearly and clearly on their content. Thanks, I hope I made it clear that I'm actually quite new to the Cocoa infrastructure.

+4
source share
2 answers

You should be able to load nib files into your windows in the init method for your different classes. For example, in a profile, you can do something like this:

-(id)init { if (self = [super init]) { NSArray *array; BOOL success = [[NSBundle mainBundle] loadNibNamed:@"ProfileWindow" owner: self topLevelObjects:&array]; if (success) { for (id obj in array) { if ([obj isKindOfClass:[NSWindow class]]) { self.profileWindow = obj; } } [self.profileWindow makeKeyAndOrderFront:self]; } } return self; } 

profileWindow - property (typed as strong). In the xib file, I installed File Owner in the profile.

+6
source

I just like improving the rdelmar solution.

You do not need to iterate over the array to find the NSWindow class. If you define profileWindow as an outlet and connect it to IB, call

 [[NSBundle mainBundle] loadNibNamed:@"ProfileWindow" owner:self topLevelObjects:&array]; 

will assign a window object to your outlet; no array material is required. The key here is the owner object, which acts as an interface. In IB you can define the type of the owner class, and if so, see Its outputs.

+2
source

Source: https://habr.com/ru/post/1436689/


All Articles