【问题标题】:Implement custom UITabBar to multiple UIViewController's为多个 UIViewController 实现自定义 UITabBar
【发布时间】:2013-05-31 08:11:20
【问题描述】:

我正在尝试在我的应用程序的多个视图控制器中包含一个标签栏,上面有 4-5 个标签栏项目,并将用作在视图(地图、关于、收藏夹等)之间跳转的菜单。

我在 Storyboard 上创建了一个 UITabBar 项目并设置了它的 Bar 项目的标签。因为相同的选项卡栏将用于其他几个视图控制器(Main、View2、View3 等),所以我决定创建一个扩展 UITabBar 的类。这将帮助我以后自定义栏。 Storyboard 中的 UITabBar 对象现在是此类 (BottomTabBar) 的对象。

我的问题是,如何检测何时点击了条形项?

另外,由于我对 TabBar 不熟悉,如果您有任何通用指南或提示可以在开发过程中帮助我,请与我分享。

BottomTabBar.h

#import <UIKit/UIKit.h>

@interface BottomTabBar : UITabBar <UITabBarDelegate>

@end

BottomTabBar.m

#import "BottomTabBar.h"

@implementation BottomTabBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
    }
    return self;
}

- (void) tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{

    NSLog(@"Tabbed!");
}

@end

MainViewController.h

#import <UIKit/UIKit.h>
#import "BottomTabBar.h"

@interface MainViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{

    AppDelegate *appDelegate;
    NSArray *searchResults;
}

@property (strong, nonatomic) IBOutlet UIScrollView *slideshow;
@property (strong, nonatomic) IBOutlet UIPageControl *scroll;
@property (strong, nonatomic) IBOutlet BottomTabBar *bottomBar;


@end

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      请参阅本教程和自定义标签栏的源代码。

      Custom tabbar

      希望对你有帮助。

      【讨论】:

      • 所以,我必须将我的课程从UITabBar 修改为UITabBarController
      • 问题是,我不想要 Tab Bar Controller,只是简单的 Tab Bar 将它包含在现有的 View Controller 中
      • 看看这个网址
      • 我的错!我做错了。我在 BottomTabBar 类中包含了委托,而不是我希望它出现的类。现在我可以检测是否按下了选项卡和标签#。感谢您的宝贵时间。
      • 我还有一个编码问题。我有 2-3 个 ViewController,我想在其中包含 UITabBar。我目前发现的方法需要,1)在每个 ViewController 上包含 UITabBarDelegate 和 2)在每个 ViewController 上包含 didSelectItem。大多数时候,每个 TabBarItem 的操作在任何地方都将保持不变。此外,即使从一个视图移动到另一个视图,我也想保留选定的项目。有什么办法可以处理所有这些重复的代码?
      猜你喜欢
      • 2011-02-03
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多