cocoa - NSWindowController 多 Nib 问题

标签 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

主 Controller .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

第二个 Controller .h

#import <Cocoa/Cocoa.h>


@interface SecondController : NSWindowController {
    NSWindowController *mController;
}
-(id)initWithMController:(NSWindowController *)mctrl;
-(IBAction)testButtonClick:(id)sender;

@end

问题所在:

第二 Controller .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 ?有人可以帮忙吗?

谢谢

最佳答案

尝试在这两个地方(除了 mController 之外)记录 self。我的猜测是,您会发现您有多个 SecondController 对象(也许一个是通过 -[MainControllerexecuteButtonClick:] 在代码中创建的,另一个是在 SecondWindow Nib 中创建的?)。

关于cocoa - NSWindowController 多 Nib 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5934545/

相关文章:

macos - 如何使用 Mail/Finder 之类的控件创建工具栏

objective-c - NSTextField 点击率?

cocoa - 在 OpenGL 中用颜色渲染的灰度纹理

c++ - OSX 10.9 上用于绘制球体等图元的 Glut/Glu 替代品

ios - 使用 Xcode Storyboards 实现丰富的旋转 UI

iphone - 更早地检测有问题的 XIB View

objective-c - 如何更改自定义 NSTextfield 的绘制矩形内的文本颜色

objective-c - 如何实现像 Number 和 Pages 中那样的 Split View?

cocoa - 从 NSTextView 中删除文本

ios - 处理错误的表达