将自定义按钮添加到导航控制器
我试图将3个自定义按钮添加到我的视图顶部的导航控制器工具栏中,或者添加3个选项的分段控件。当我创建我的视图控制器(fwc)但按钮不出现时,我的应用程序委托上有以下代码。将自定义按钮添加到导航控制器
/* 设置导航控制器的进给翼片 */
// instantiate the feedingViewController and set the title to Feedings
feedingViewController *fwc =
[[feedingViewController alloc] initWithNibName:@"feedingViewController"
bundle:[NSBundle mainBundle]];
//fwc.title = @"Feedings";
// set the tab bar item up and add it as feedingViewController's tab bar item
UITabBarItem *feedingTabBarItem =
[[UITabBarItem alloc] initWithTitle:@"Feedings" image:nil tag:0];
fwc.tabBarItem = feedingTabBarItem;
[feedingTabBarItem release];
// create a new nav controller for feedings and add root view
feedingNavController = [[UINavigationController alloc] init];
//Create the add button, need to change the selector to something though *****
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(newFeeding)];
//self.navigationItem.rightBarButtonItem = add;
UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
// Create and configure the segmented control
UISegmentedControl *sortToggle = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:@"Ascending",@"Descending", nil]];
sortToggle.segmentedControlStyle = UISegmentedControlStyleBar;
sortToggle.selectedSegmentIndex = 0;
[sortToggle addTarget:self action:@selector(toggleSorting:)forControlEvents:UIControlEventValueChanged];
// Create the bar button item for the segmented control
UIBarButtonItem *sortToggleButtonItem = [[UIBarButtonItem alloc]initWithCustomView:sortToggle];
[sortToggle release];
// Set our toolbar items
feedingNavController.toolbarItems = [NSArray arrayWithObjects:
flexibleSpaceButtonItem,
sortToggleButtonItem,
flexibleSpaceButtonItem,
add,
nil];
feedingNavController.navigationController.navigationBarHidden=NO;
[sortToggleButtonItem release];
[add release];
// Push the feedingViewController on the nav stack and release it.
[feedingNavController pushViewController:fwc animated:NO];
[fwc release];
为了使用中的UITabBar一样,你需要一个的UITabBarController,这比UINavigationController的不同。 UITabBar与UISegmentedControl有着根本不同的用途。看起来您尝试实现的功能不适用于UITabBar。在您的问题描述中,您提到尝试将这些按钮添加到“顶部的导航控制器工具栏”中。一个UINavigationController有一个UINavigationBar,它是一个横跨顶部的横条,还有一个UIToolbar,它是出现在底部的横条。默认情况下,UIToolbar被设置为隐藏,但是当您创建UINavigationController时(参见Xcode中的UINavigationController引用),您将获得一个UIToolbar。
Apple的NavBar演示显示了如何将UISegmentedControl放入UINavigationBar。取而代之的是标题,使用自定义titleview的显示分段控制:
fwc.navigationItem.titleView = sortToggle;
如果你想放置在UINavigationBar的外接的UIBarButtonItem,以及,你可以使用:
fwc.navigationItem.rightBarButtonItem = add;
请注意,您实际上不应该试图自己定制UINavigationController的导航栏。定制的正确方法是让一个单独的视图控制器访问它自己的navigationItem,然后用你想要的项目设置titleView和rightBarButtonItem。
如果你想使用UIToolBar代替,这意味着你的项目将出现在屏幕的底部接近你的问题,你可以做这样的事情:
// Assume UIBarButtonItem *add, UIBarButtonItem *sortToggleButtonItem,
// and UIBarButtonItem *flexibleSpaceButtonItem are allocated
[fwc setToolbarItems:[NSArray arrayWithObjects:
flexibleSpaceButtonItem,
sortToggleButtonItem,
flexibleSpaceButtonItem,
add,
nil]];
[feedingNavController setToolbarHidden:NO];
感谢您的回答,您提出的建议确实有效,但我宁愿将UI元素放在顶部。我决定在视图顶部添加一个分段控件并使用它。不过谢谢。 – adam0101 2010-07-25 17:50:38
你为什么注释掉该行自.navigationItem.rightBarButtonItem =添加? – 2010-07-25 03:08:57