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/