New NSWindow from application - mission impossible

2020-05-08 06:14发布

OK, what am I doing wrong?
1. Created cocoa app and appDelegate named: window2AppDelegate
2. window2AppDelegate.h

#import "PrefWindowController.h"
@interface window2AppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    PrefWindowController * ctrl;
}

@property (assign) IBOutlet NSWindow *window;
- (IBAction) buttonClick:(id)sender;
- (IBAction) buttonCloseClick:(id)sender;
@end


3. in xib editor, window connected to window controller - set to appdelegate, buttonclick actions to buttons
4, created

#import <Cocoa/Cocoa.h>
@interface PrefWindowController : NSWindowController {
@private

}
@end

#import "PrefWindowController.h"
@implementation PrefWindowController

- (id)init {
    self = [super initWithWindowNibName: @"PrefWindow"];
    return self;
}

- (void)dealloc {
    // Clean-up code here.
    [super dealloc];
}

- (void)windowDidLoad {
    [super windowDidLoad];
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

@end


5. created new xib file named PrefWindow window IBOutlet connected to window from its controller (also controller set to PrefWindowController) Option "Visible At Launch" UNCHECKED! i want to see this window on buttonclick.
6. implemented window2AppDelegate

#import "window2AppDelegate.h"
@implementation window2AppDelegate
@synthesize window;

- (id) init {
    if ((self = [super init])) {
        ctrl = [[PrefWindowController alloc] init];
    if ([ctrl window] == nil)
        NSLog(@"Seems the window is nil!\n");
    }
    return self;
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
    return YES;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
}

- (IBAction) buttonClick:(id)sender {
//    [[ctrl window] makeKeyAndOrderFront:self]; this doesn't work too :(
NSLog(@"it is here");
[ctrl showWindow:sender];
}

- (IBAction) buttonCloseClick:(id)sender {
    [window close];
}

@end


7. After I build and run app: closebutton closes the app but buttonclick - won't show me PrefWindow!? Why and what am i doing wrong? Don't dell me that to show other window in cocoa objective-c is more difficult than in "stupid" Java or C#?

3条回答
The star\"
2楼-- · 2020-05-08 06:47

I'd suggest you move the creation of the PrefWindowController to applicationDidFinishLaunching:

I am not sure the application delegate's init method is called. Probably only initWithCoder: gets called when unarchiving the object from the NIB.

查看更多
叼着烟拽天下
3楼-- · 2020-05-08 06:53

Finally i've managed the problem! In the nib editor for PrefWindow I had to do: Set File's owner class to: NSWindowController then connect window IBOutlet from File's owner to my (preferneces) window. After 6 days of unsuccessful attempts, google works.
Anyway, thanks for all your responses and time!

查看更多
我想做一个坏孩纸
4楼-- · 2020-05-08 07:05
- (id) init {
   if ((self = [super init])) {
      ctrl = [[PrefWindowController alloc] init];
   if ([ctrl window] == nil)
      NSLog(@"Seems the window is nil!\n");
   }
   return self;
}

init is way too early in the scheme of things to be trying to test IBOutlets. They will still be nil yet. Not until later on in the object creation process will the nib outlets be "hooked up". The standard method where you can know that everything in the nib file has been hooked up is:

- (void)awakeFromNib {

}

At that point, all of your IBOutlets should be valid (provided they're not purposely referencing an object in a separate, yet-unloaded nib).

If PrefWindowController is a class that will only be used after the user chooses Preferences from the app menu, I would have to disagree with the others and say that I would not create the instance of the PrefsWindowController at all during the initial load. (Your main controller should be able to function independently from the prefs window). If you have a method that is meant to load the preferences window, then when that method is called, you should check to see if the PrefsWindowController instance is nil, and if it is, create it, then proceed to show the prefs window.

查看更多
登录 后发表回答