TableData 是 NSObject 的子类,作为基于视图的表的数据源和表委托。TableData的awakeFromNib
方法会运行很多次,因为我使用的是基于视图的表。如果 TableData 是 NSViewController 的子类,我可以loadView:
用来完成我的任务,但 TableData 是 NSObject 的子类,我的问题是:
- 我应该使用哪种方法而不是
awakeFromNib
初始化 TableData 属性?
我不知道你如何创建你的窗口,但你可以这样做:
AppDelegate.m
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
fMainWinDelegate = nil;
fMainWinDelegate = [[MainWinDelegate alloc] init];
[fMainWinDelegate showWindow];
}
MainWindowDelegate.m
- (id)initWithWindow:(NSWindow *)AWindow
{
NSLog(@"MainWinDelegate::initWithWindow");
self = [super initWithWindow:AWindow];
if (self) {
// Initialization code here.
NSLog(@"MainWinDelegate::initWithWindow, we have self!");
}
return self;
}
- (void)awakeFromNib
{
NSLog(@"MainWinDelegate::awakeFromNib");
// only for debug and to be sure that is called many times
}
- (void)showWindow {
NSLog(@"MainWinDelegate::showWindow");
if (!self.window) {
[NSBundle loadNibNamed:@"MainWin" owner:self];
NSLog(@"MainWinDelegate::showWindow init part");
// do your init here
}
[self.window makeKeyAndOrderFront:self];
NSLog(@"MainWinDelegate::showWindow end");
}
这是日志:
MainWinDelegate::initWithWindow
MainWinDelegate::initWithWindow, we have self!
MainWinDelegate::showWindow
MainWinDelegate::awakeFromNib
MainWinDelegate::showWindow init part
MainWinDelegate::showWindow end
您可以选择:
@interface MONTableData : NSObject
// a designated initializer:
- (id)init;
- (id)initWithCoder:(NSCoder *)pCoder;
// or when the `TableData`'s input data source is set:
- (void)setPhotoAlbum:(MONPhotoAlbum *)pPhotoAlbum;
@end