2011-11-29 138 views
0

這是一個簡單的問題,但我是新的xcode dev。我在網上按照指南在導航欄上有多個按鈕。 navgivation欄上的編輯按鈕有一個名爲「editButton」的IBAction方法。其中有一個(id)sender作爲參數。如何獲取發件人並將編輯中的文本更改爲完成,然後進行編輯?uitoolbar在導航欄中。

"UIBarButtonitem *bbi = (UIBarButonItem *) sender;"似乎沒有工作。我如何獲得導航欄工具欄中的按鈕?

謝謝。

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)]; 

// create the array to hold the buttons, which then gets added to the toolbar 
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3]; 

// create a standard "add" button 
UIBarButtonItem* bi = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL]; 
bi.style = UIBarButtonItemStyleBordered; 
[buttons addObject:bi]; 
[bi release]; 

// create a spacer 
bi = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 
[buttons addObject:bi]; 
[bi release]; 

// create a standard "EDIT" button 
bi = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editButton:)]; 
bi.style = UIBarButtonItemStyleBordered; 
[buttons addObject:bi]; 
[bi release]; 

// stick the buttons in the toolbar 
[tools setItems:buttons animated:NO]; 

[buttons release]; 

// and put the toolbar in the nav bar 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
[tools release]; 



-(IBAction)editButton:(id) sender{ 
UIBarButtonitem *bbi = (UIBarButonItem *) sender; 

if (bbi title isequalsString:@"Done){ 
[bbi setTitle:@"Edit"]; 
} 
}else{ 
[bbi setTitle:@"Done"]; 
} 

} 

回答

0

麻煩的是,你用了UIBarButtonSystemItemEdit,而不是標準uibarbutton。 嘗試與創建它:

bi = [UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(editButton:)]; 

然後使用代碼的其餘部分不變。

+0

那麼我如何在editButton方法中獲得雙對象? – kraitz

+0

就像你現在做的那樣 - 通過從發件人轉換它:UIBarButtonitem * bbi =(UIBarButonItem *)sender; –

0

UIBarButtonSystemItemEdit是一個特殊的酒吧按鈕項目,照顧你的「編輯/完成」狀態。無需手動更改按鈕的文本。

+0

我試過的狀態沒有改變.. – kraitz

+0

我其實是錯過了。如果您使用的是UIViewController子類,則應該可以使用內置的編輯按鈕屬性。 '[self editButtonItem]' – cocoahero

+0

我不相信你可以做到這一點,並有多個按鈕... –