【问题标题】:Add Custom buttons to Navigation Controller将自定义按钮添加到导航控制器
【发布时间】:2010-07-25 13:15:40
【问题描述】:

我正在尝试向视图顶部的导航控制器工具栏添加 3 个自定义按钮,或者添加具有 3 个选项的分段控件。当我创建视图控制器(fwc)但按钮不出现时,我的应用程序委托上有以下代码。

/* 为 Feeding 选项卡设置导航控制器 */

// 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];

【问题讨论】:

  • 为什么注释掉 self.navigationItem.rightBarButtonItem = add 这一行?

标签: iphone objective-c cocoa-touch uikit


【解决方案1】:

为了使用 UITabBar,您需要一个 UITabBarController,它与 UINavigationController 不同。 UITabBar 与 UISegmentedControl 有根本不同的用途。您尝试实现的功能似乎不适用于 UITabBar。在您的问题描述中,您提到尝试将这些按钮添加到“顶部的导航控制器工具栏”。 UINavigationController 有一个 UINavigationBar,它是横跨顶部的栏,还有一个 UIToolbar,它是出现在底部的栏。默认情况下,UIToolbar 设置为隐藏,但无论何时创建 UINavigationController,您都会免费获得一个 UIToolbar(请参阅 Xcode 中的 UINavigationController 参考)。

Apple 的 NavBar 演示展示了如何将 UISegmentedControl 放入 UINavigationBar。代替标题,使用自定义 titleView 来显示分段控件:

fwc.navigationItem.titleView = sortToggle;

如果你想把你添加的 UIBarButtonItem 也放在 UINavigationBar 中,你可以使用:

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 元素放在顶部。我决定在视图顶部添加一个分段控件并使用它。不过还是谢谢。
猜你喜欢
  • 2021-06-27
  • 2016-09-15
  • 2011-08-03
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多