這是一個簡單的問題,但我是新的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"];
}
}
那麼我如何在editButton方法中獲得雙對象? – kraitz
就像你現在做的那樣 - 通過從發件人轉換它:UIBarButtonitem * bbi =(UIBarButonItem *)sender; –