【发布时间】:2016-11-13 13:30:26
【问题描述】:
MacOs 10.10 下使用 Xcode 7.3 使用 Objective c 的简单测试应用程序。
AppDelegate.h
@property MainWindowController *myMainWindowController;
AppDelegate.m
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
_myMainWindowController = [[MainWindowController alloc] initWithWindowNibName:@"MainWindowController"];
[_myMainWindowController showWindow:self];
}
MainWindowController.xib 只是带有主视图的 MainWindow
MainWindowController.h
@property IBOutlet NSView * mainView; //hooked up to the View in the MainWindowController.xib
MainWindowController.m
- (void)windowDidLoad {
[super windowDidLoad];
MyXib* vc = [[MyXib alloc] initWithNibName:nil bundle:nil];
[_mainView addSubview:[vc view] ];
}
MyXib 作为 NSViewController 的子类在 Xcode 中创建。它的视图中有一个 Button 对象。
MyXib.h
@interface MyXib : NSViewController
- (IBAction)buttonPushed:(id)sender;
MyXib.m
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)buttonPushed:(id)sender{
NSLog(@"buttonPushed");
}
theButton 的动作连接到 buttonPushed。
运行应用程序时,用户界面按预期显示。但是当单击 Button 时,程序在没有调用 buttonPushed 方法的情况下崩溃。有时调试窗口会显示堆栈爬取以类似“[NSURL buttonPushed:]: unrecognized selector sent to instance..”之类的结尾。
我已经调用了 viewDidLoad、awakeFromNib 和 init。这些调用中的断点似乎显示了根据文档的逻辑行为。
【问题讨论】:
-
没有任何东西保留在视图控制器上,它被释放了。
-
防止它被解除分配的最简单方法是什么?
-
在 MainWindowController.m 中添加属性
MyXib* vc。
标签: objective-c xcode cocoa