2012-08-01 33 views
5

我想在我的UIToolBar上單擊完成按鈕時調出「TableViewController」的.nib。但下面的內容不允許點擊來創建新視圖。我該如何糾正這一點?請告訴我我錯在哪裏,應該替換什麼以及爲什麼。如何給barbuttonitem行動?

//Here's the selector in my overlay. 
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: 
UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed)]; 

//Here's how I made my action. Btw, the uitoolbar has no nib, it's an overlay on the 
//(camera mode). 

-(void)doneButtonPressed { 
TableViewController *tableView = [[TableViewController alloc] 
initWithNibName:@"TableViewController" bundle:nil]; 
tableView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
[self presentModalViewController:tableView animated:YES]; 
} 


//Yet nothing happens when I click on my done button on my overlay. And I've made sure 
// i've imported .h frameworks correctly too. 

假設你是從barbuttonitem這是一個UItoolbar覆蓋帶來了一個筆尖。你會怎麼做?

我被告知,爲了使它正常工作,我必須添加[barButtonItem addTarget:self action:@selector(doneButtonPressed)forControlEvents:UIControlEventTouchUpInside]; 。

但是,如果我添加它,我得到這個:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: 
UIBarButtonSystemItemDone addTarget:self action:@selector(doneButtonPressed) 
forControlEvents:UIControlEventTouchUpInside]; 

導致我得到一個錯誤閱讀「實例方法‘ - initWithBarButtonSystemItem:目標:動作:forControlEvents:’未找到(返回類型默認爲' id')「

除了向我展示正確的添加劑之外,請向我展示除我在此處編寫的代碼之外的解決方案。

回答

5

如果您使用的是XCode 4,您可以簡單地按住Ctrl +拖動BarButtonItem到您的.h文件,並且您可以從那裏自動創建IB動作。

+0

我以編程方式製作barbuttonitem,因爲它所在的工具欄是以編程方式製作的,所以使用IB並不可行。 – Capricorn 2012-08-01 19:06:37

1

A UIBarButtonItem遵循默認UIControlEventTouchUpInside,您無法設置它。 Xcode中應該自動提示分配它的方法,但正確的代碼是:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed)]; 

注意,沒有forControlEvents:

+0

那麼我應該懷疑,即使在我根據您的建議糾正後,void方法不會執行切換到新視圖? – Capricorn 2012-08-01 19:24:36

+1

首先,設置一個斷點並查看該方法是否被調用。還有:'[present presentViewController:tableView animated:YES completion:nil];' – runmad 2012-08-01 19:27:21

1

嘗試這些變化:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)]; 

-(void)doneButtonPressed:(id)sender 
{ 
} 

目標是通過發起行動的對象(即本身的UIBarButtonItem)

其次,在您的doneButtonPressed功能設置斷點。例如,如果tableView被設置爲零,那麼你將看不到任何事情發生。也許在實例化這個視圖控制器時存在問題。