2011-03-26 103 views
1

我與我創建(MyViewController),有一個名爲「按鈕」的UIButton一個視圖控制器工作,這是我一直在試圖獲取代碼它的工作:爲什麼在我調用init方法的超級視圖的時候,這個UIButton沒有被初始化?

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { 
MyViewController *myViewController=[[[MyViewController alloc] init] autorelease]; 
[myViewController.button addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside]; 
return footerController.view; 
} 

我不能確定什麼不對的地方,但顯然我可以修復它,如果我這樣做:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { 
MyViewController *myViewController=[[[MyViewController alloc] init] autorelease]; 
[myViewController.view addSubview:nil]; 
[myViewController.button addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside]; 
return footerController.view; 
} 

有人能告訴我爲什麼會這樣呢?有沒有辦法解決這個問題,因爲我認爲不這樣做..

回答

0

只有在需要保存內存時纔會加載筆尖,如view屬性被訪問的信號。如果可能的話,最好在MyViewController的viewDidLoad方法中進行這種初始化。

0

什麼Anomie說的是對的..我找到了解決方法..我不能直接訪問按鈕與myViewController.button,而不得不使用方法:[myViewController.view viewWithTag:buttonTag]。這樣我確保按鈕已經被初始化。

相關問題