如何将几个UIBarButtonItem添加到NavigationBar?

Kha*_*man 9 iphone objective-c uinavigationbar uinavigationitem ios

我想在a上绘制多个按钮UINavigationBar.这些将在右侧或左侧.

roh*_*tel 10

我做了一个例子,我在NaviagationBar的右侧有两个按钮(即编辑和+).

1)你必须创建一个NSMutableArray(例如"按钮")并添加UIBarButtonItem(例如bi1和bi2)NSMutableArray(即按钮).

2)将NSMutableArray(即示例中的按钮)添加到工具栏(即UIToolbar *tools示例中).

3)将工具栏添加到NavigationBar.

 NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
 UIToolbar *tools = [[UIToolbar alloc]
                    initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add bar button1.

UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
//[bi1 release]; Do not release if ARC enabled.

// Add bar button2.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
//[bi2 release]; Do not release if ARC enabled.

// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
//[buttons release];  Do not release if ARC enabled.

 // Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
//[twoButtons release]; Do not release if ARC enabled.
Run Code Online (Sandbox Code Playgroud)