iPhone——向用户在 moreNavigationController 中选择选项卡时创建的导航栏添加一个按钮

发布于 2024-09-11 02:56:36 字数 209 浏览 4 评论 0原文

我的标签栏有十个标签。因此,其中六个被推入“更多”选项卡。其中一些没有 UINavigationControllers。即,该选项卡由 UIViewController 子类控制,该子类不是导航控制器。

当用户选择其中之一时,相应的视图控制器就会出现,顶部有一个 UINavigationBar。

我想向该导航栏添加一个按钮。我怎样才能做到这一点?

谢谢。

My tab bar has ten tabs. Six of these are therefore shoved into the "More" tab. Several of them do not have UINavigationControllers. I.e, the tab is controlled by a UIViewController subclass that is not a navigation controller.

When the user selects one of these, the appropriate view controller come sup, with a UINavigationBar at the top.

I want to add a button to that navigation bar. How can I do that?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日情怀 2024-09-18 02:56:37

好吧,如果您想以编程方式执行此操作,那么我建议将 UITabBar 中的每个 viewController 替换为容纳相应视图控制器的 UINavigationController。

所以,你的旧代码看起来像这样:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  UITabBarController *tbc = [[[UITabBarController alloc]init]autorelease];
  [window addSubview:tbc.view];
  UIViewController *mapVC = [[[UIViewController alloc] init]autorelease];
  NSArray *tabViewControllerArray = [NSArray arrayWithObjects:self.mapVC, nil];
  tbc.viewControllers = tabViewControllerArray;
}

新代码应该是:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  UITabBarController *tbc = [[[UITabBarController alloc]init]autorelease];
  [window addSubview:tbc.view];
  UIViewController *mapVC = [[[UIViewController alloc] init]autorelease];
  // add the viewController to a UINavigationController
  UINavigationController *mapNav = [[[UINavigationController alloc] initWithRootViewController:mapVC]autorelease];
  // put the nav controller in the array instead
  NSArray *tabViewControllerArray = [NSArray arrayWithObjects:mapNav, nil];
  tbc.viewControllers = tabViewControllerArray;

  // this code adds a right button to the mapBav navigationBar
  // this uses a custom view, but you could use a standard UIBarButtonItem too 
  NSArray *items = [NSArray arrayWithObjects: [UIImage imageNamed:@"flag-icon.png"], nil];
  UISegmentedControl *tableControl = [[[UISegmentedControl alloc] initWithItems:items]autorelease];
  tableControl.segmentedControlStyle = UISegmentedControlStyleBar;
  UIBarButtonItem *segmentBarItem = [[[UIBarButtonItem alloc] initWithCustomView:tableControl] autorelease];
  self.navigationItem.rightBarButtonItem = segmentBarItem;
}

Well, if you want to do this programmatically, then I would suggest replacing each of your viewControllers in your UITabBar with UINavigationControllers that house the respective view controllers.

So, your old code looks something like this:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  UITabBarController *tbc = [[[UITabBarController alloc]init]autorelease];
  [window addSubview:tbc.view];
  UIViewController *mapVC = [[[UIViewController alloc] init]autorelease];
  NSArray *tabViewControllerArray = [NSArray arrayWithObjects:self.mapVC, nil];
  tbc.viewControllers = tabViewControllerArray;
}

New code should be:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  UITabBarController *tbc = [[[UITabBarController alloc]init]autorelease];
  [window addSubview:tbc.view];
  UIViewController *mapVC = [[[UIViewController alloc] init]autorelease];
  // add the viewController to a UINavigationController
  UINavigationController *mapNav = [[[UINavigationController alloc] initWithRootViewController:mapVC]autorelease];
  // put the nav controller in the array instead
  NSArray *tabViewControllerArray = [NSArray arrayWithObjects:mapNav, nil];
  tbc.viewControllers = tabViewControllerArray;

  // this code adds a right button to the mapBav navigationBar
  // this uses a custom view, but you could use a standard UIBarButtonItem too 
  NSArray *items = [NSArray arrayWithObjects: [UIImage imageNamed:@"flag-icon.png"], nil];
  UISegmentedControl *tableControl = [[[UISegmentedControl alloc] initWithItems:items]autorelease];
  tableControl.segmentedControlStyle = UISegmentedControlStyleBar;
  UIBarButtonItem *segmentBarItem = [[[UIBarButtonItem alloc] initWithCustomView:tableControl] autorelease];
  self.navigationItem.rightBarButtonItem = segmentBarItem;
}
爱的那么颓废 2024-09-18 02:56:37

有两种方法可以解决这个问题。

  1. 只需为每个选项卡使用 UINavigationController,并在其中包含视图控制器。但听起来您不想这样做,所以:

  2. 使用 Interface Builder 放置一个 UINavigationBar。 导航栏 http://cl.ly/395fb8c0a9e9df781897/content
    然后,您可以拖入 UIBarButtonItems 并根据您的喜好配置它们,也在 IB 中。 按钮 http://cl.ly/7501d3e8e5d57dac5e00/content

There are two ways to go about it.

  1. Just use a UINavigationController for each tab, with your view controller in it. But it sounds like you don't want to do this, so:

  2. Put a UINavigationBar in using Interface Builder. nav bar http://cl.ly/395fb8c0a9e9df781897/content
    You can then drag in UIBarButtonItems and configure them to your liking, also in IB. buttons http://cl.ly/7501d3e8e5d57dac5e00/content

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文