NSWindowController多个笔尖问题

时间:2011-05-09 08:44:07

标签: cocoa interface-builder

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:(id)sender;

@end

问题在于:

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

日志:

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

为什么mController变为null?有人可以帮忙吗?

由于

1 个答案:

答案 0 :(得分:1)

尝试在两个地方都记录self(除了mController)。我的猜测是你会发现你有多个SecondController对象(可能是通过 - [MainController executeButtonClick:]代码创建的,另一个是SecondWindow nib中的另一个)。