2012-09-04 82 views
0

enter image description here可可:該方法我應該使用來初始化,而不是awakeFromNib

資料表屬性是NSObject的的一個子類作爲數據源和表代表了基於視圖的表。 TableData的awakeFromNib方法會運行很多次,因爲我使用的是基於視圖的表。如果資料表是NSViewController的一個子類,我可以使用loadView:來完成我的任務,但資料表是NSObject的子類,我的問題是:

  1. 我應該使用哪種方法,而不是awakeFromNib初始化資料表的屬性?
+0

** awakeFromNib將運行多次**你能肯定嗎? – Mil0R3

+0

是的,因爲每個基於視圖的表格單元格都會調用它。 – NOrder

+0

從你的筆尖文件我想它是在一個windowController,如果是這樣的嘗試' - (void)windowDidLoad'方法 – Mil0R3

回答

1

我不知道你如何創建你的窗口,但你可以這樣做:

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 
0

可以選擇任何一種:

@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 
相關問題