2011-04-30 131 views
0
- (id)initWithStyle:(UITableViewStyle)style { 
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
    self = [super initWithStyle:style]; 
    if (self) { 
     myArray = [[NSMutableArray alloc]init]; 
     NSLog(@"ARRAY INITIALIZED"); //THIS NEVER OCCURS 
    } 
    return self; 
} 
+0

而如果你把NSLog放在函數的開頭呢? – mathk 2011-05-01 01:05:14

+0

函數只是不被調用。我什麼也沒得到。 – jini 2011-05-01 02:44:21

回答

1

我會建議只是把你的初始化代碼在viewDidLoadviewDidAppear。你需要確保你不會多次(和進程中的孤立內存)alloc,但它會被調用,不管它是以xib還是以編程方式裝配。

- (void)viewDidLoad 
{ 
    if(!myArray) 
    { 
     myArray = [[NSMutableArray alloc] init]; 
    } 
} 
1

我真的不能從給定的代碼告訴,但如果你讓你的控制器編程,確保你初始化你的子類:

[[MyCustomTableViewController alloc] initWithStyle:style]; 

不是UITableViewController

[[UITableViewController alloc] initWithStyle:style]; 
3

您是以編程方式創建表還是在Interface Builder中創建表?如果它在界面構建器中,那麼您需要改寫-(id) initWithCoder:(NSCoder *)coder

+0

我只是繼承。 @interface InboxTableViewController:UITableViewController { \t \t NSMutableArray * myArray; } – jini 2011-05-01 00:08:13

0

發現initWithStyle永遠不會被調用。我忘了提,初始窗口建立IB,所以我自定義控制器從廈門國際銀行叫,我必須覆蓋的initWithCoder ....

http://www.iosdeveloperforums.com/thread-initwithstyle-overriding

+0

如果是這種情況,那麼initWithCoder被調用,你需要移動你的init代碼。 – slycrel 2011-05-01 23:38:42

+0

這幾乎就是我的答案所說的。 – 2011-05-03 14:52:11

相關問題