开发者

NSWindowController multiple nib problem

MainController.h

#import <Cocoa/Cocoa.h>


@interface MainController : NSWindowController {
    NSWindowController *sc;
    IBOutlet NSTextField *txt1;
}

-(IBAction)executeButtonClick:(id)sender;
-(void)setTxt1Text:(NSString *)txt;

@end

MainController.m

#import "MainController.h"
#import "SecondController.h"


@implementation MainController

-(void)awakeFromNib
{
    sc = nil;
}

-(IBAction)executeButtonClick:(id)sender;
{
    if (sc == nil)
    {
        sc = [[SecondController alloc] initWithMController:self];
    }
    [sc showWindow:self];
    [[sc window] makeKeyAndOrderFront:sender];
}

-(void)setTxt1Text:(NSString *)txt;
{
    [txt1 setStringValue:txt];
}

@end

SecondController.h

#import <Cocoa/Cocoa.h>


@interface SecondController : NSWindowController {
    NSWindowController *mController;
}
-(id)initWithMController:(NSWindowController *)mctrl;
-(IBAction)testButtonClick:开发者_StackOverflow中文版(id)sender;

@end

Here's the problem reside:

SecondController.m

#import "SecondController.h"


@implementation SecondController

    -(id)initWithMController:(NSWindowController *)mctrl;
    {
        self = [super initWithWindowNibName:@"SecondWindow"];
        mController = mctrl;
        NSLog(@"%@",mController);
        return self;
    }

    -(IBAction)testButtonClick:(id)sender;
    {
        NSLog(@"%@",mController);
        [mController setTxt1Text:@"Test Success"];
    }

    @end

Logs:

2011-05-09 15:41:10.337 MultiWindow[4334:a0f] <MainController: 0x1005295b0>
2011-05-09 15:41:11.336 MultiWindow[4334:a0f] (null)

Why mController became null ? Can anyone help ?

Thanks


Try logging self in both places as well (in addition to mController). My guess is you will find that you have more than one SecondController object (perhaps one created in code through -[MainController executeButtonClick:] and the other in your SecondWindow nib?).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜