2011-11-29 142 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

我不相信你可以做到这一点,并有多个按钮... –