导航栏中的uitoolbar。

时间:2011-11-29 02:13:35

标签: iphone xcode ios4

这是一个简单的问题,但我是xcode dev的新手。我在线跟踪指南,在导航栏上有多个按钮。导航栏上的编辑按钮有一个名为“editButton”的IBAction方法。其中(id)sender为参数。获取发件人并将文本从编辑更改为完成,完成编辑?

"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"];
}

}

2 个答案:

答案 0 :(得分:0)

麻烦的是你使用了 UIBarButtonSystemItem Edit ,而不是标准的uibarbutton。 尝试使用:

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

然后按原样使用其余代码。

答案 1 :(得分:0)

UIBarButtonSystemItemEdit是一个特殊的条形按钮项,负责处理“编辑/完成”状态。无需手动更改按钮的文本。