如何将多个UIBarButtonItem添加到UINavigationBar?

时间:2012-03-26 05:54:08

标签: objective-c cocoa-touch ipad uinavigationbar uibarbuttonitem

我想向UIBarButtonItem添加许多UINavigationbar,而不仅仅是左右按钮:

logoButton = [[UIBarButtonItem alloc] initWithTitle:@"A Button" style:UIBarButtonItemStyleBordered target:self action:@selector(logoButtonAClicked:)];

logoButton2 = [[UIBarButtonItem alloc] initWithTitle:@"B Button" style:UIBarButtonItemStyleBordered target:self action:@selector(logoButtonBClicked:)];

logoButto3 = [[UIBarButtonItem alloc] initWithTitle:@"C Button" style:UIBarButtonItemStyleBordered target:self action:@selector(logoButtonCClicked:)];

self.navigationController.navigationBarHidden = NO;

self.title = @"Title";

NSArray* items = [[NSArray alloc] initWithObjects:logoButtonA, logoButtonB, logoButtonC, nil];
self.navigationController.navigationBar.items = items;

我在SIGBRT

上获得self.navigationController.navigationBar.items = items;

如何将多个UIBarButtonItem添加到UINavigationBar

3 个答案:

答案 0 :(得分:17)

您需要将UIBarButtonItem个实例添加到UINavigationItem,而不是UINavigationBar。所以你可以这样做:

NSArray *buttonArray = [NSArray arrayWithObjects:logoButton, logoButton2, logoButton3, nil];
self.navigationItem.leftBarButtonItems = buttonArray;

如果您想要右侧的按钮,请使用rightBarButtonItems

答案 1 :(得分:4)

你应该使用

self.navigationItem.leftBarButtonItems = items;

答案 2 :(得分:-1)

在导航栏中使用addSubView。