ios tabbar 怎么显示多个item

ios tabbar 怎么显示多个item,第1张

#import "SZMTabBarController.h"

#import "SZMTabBar.h"

#import "SZMHomeViewCtrl.h"

#import "SZMNavigationController.h"

#import "SZMDiscoerViewCtrl.h"

@interface SZMTabBarController ()<SZMTabBarDelegate>

@end

@implementation SZMTabBarController

- (void)viewDidLoad {

[super viewDidLoad]

//初始化一个自己的tabbar

SZMTabBar *tabBar = [[SZMTabBar alloc]init]

tabBar.delegate = self

//通过KVC去设置只读的属性

[self setValue:tabBar forKey:@"tabBar"]

//添加四个控制器

//首页

SZMHomeViewCtrl *homeCtrl = [[SZMHomeViewCtrl alloc]init]

[self addChildVc:homeCtrl title:@"首页" imageName:@"tabbar_home" selImgName:@"tabbar_home_selected"]

UITableViewController *messageCtrl = [[UITableViewController alloc]init]

[self addChildVc:messageCtrl title:@"消息" imageName:@"tabbar_message_center" selImgName:@"tabbar_message_center_selected"]

SZMDiscoerViewCtrl *discoveryCtrl = [[SZMDiscoerViewCtrl alloc]init]

[self addChildVc:discoveryCtrl title:@"发现" imageName:@"tabbar_discover" selImgName:@"tabbar_discover_selected"]

UITableViewController *profileCtrl = [[UITableViewController alloc]init]

[self addChildVc:profileCtrl title:@"我" imageName:@"tabbar_profile" selImgName:@"tabbar_profile_selected"]

}

//设置tabbar的一些属性

- (void)addChildVc:(UIViewController *)Controller title:(NSString *)title imageName:(NSString *)imgName selImgName:(NSString *)selImgName{

Controller.title = title

Controller.tabBarItem.image = [UIImage imageNamed:imgName]

Controller.tabBarItem.selectedImage = [UIImage imageNamed:selImgName]

self.tabBar.tintColor = [UIColor orangeColor]

SZMNavigationController *navCtrl = [[SZMNavigationController alloc]initWithRootViewController:Controller]

[self addChildViewController:navCtrl]

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning]

// Dispose of any resources that can be recreated.

}

-(void)TabBar:(SZMTabBar *)TabBar plusBtnDidClick:(UIButton *)btn{

NSLog(@"1")

}

@end

为了更好理解使用用tabbar和切换视图,我们创建一个Empty Application。

   1、 打开Xcode ,新建项目

   2、 创建View Controller

在项目上按花键+N创建新文件,创建 Objective-C class 文件,按Next按钮,subClass 选UIViewController 。勾选上xib选项

以同样方式创建另外三个ViewController ,RedViewController ,GreyViewController,YellowViewController。四个View准备好了。那么Tabbar呢?

   3、 创建TabBarController.xib文件,选择创建Empty文件

这时候你发现创建的xib文件是空白的,不用慌,去右下角控件栏中把TabBar Controller拖过来就Ok了。

4、 关联TabBarController.xib ,tabbarAppDelegate这两个文件

在上图中选择File’s Owner,打开Identity Inspector,在Class一栏选择tabbarAppDelegate

这样,我们就可以创建TabBarController.xib 文件指向tabbarAppDelegate 文件的Outlet映射了。

5、 在Xcode中的工具栏的View菜单找到 打开Assistant Editor,使tabbarAppDelegate.h和TabBarController.xib 同时打开。

在xib文件上按住control键,往tabbarAppDelegate.h,创建Outlet.

d出窗口输入 rootController,点connect。

   6、 添加代码

打开tabbarAppDelegate.m,在didFinishLaunchingWithOptions方法中添加代码:

1.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

2. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]

3. // Override point for customization after application launch.

4. [[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil]

5. [self.window addSubview:self.rootController.view]

6. self.window.backgroundColor = [UIColor whiteColor]

7. [self.window makeKeyAndVisible]

8. return YES

   7、 往TabBarController.xib上添加Tab Bar Item,

把控件栏上的Tab Bar Item控件往TabBarController.xib上拖拽即可,一个放4个。

   8 、关联Tab Bar Item和***ViewController。

选择其中一个Tab Bar Item,在右上角打开Identity Inspector,在Class中选择BlueViewController:

然后,打开Attribute,在NIB Name选择BlueViewController:

其他3个tab item重复类似的 *** 作,选中对应的ViewController,这样在切换Tab标签时,就可以切换到对应的页面。

9、 设置tab item的属性

选中其中一个tab item ,会在右上角的属性栏里看到如下信息

Badge是红色圈圈里面有数字 ,表示有多少条信息的属性

Identifier 是tab item的样式,选custom是自定义,下面的是系统的样式。我选了其中四种。

bar ITem 的title image在custom的样式下能设置。

10 、剩下的3个Tab Item也做类似的设置即可。

现在基本完工,运行看看结果如何。好吧,其实和第一第二个图是一样的`,这里就不放了。

11 、在viewDidLoad方法加Log观察切换View

可以加写日志看看对应的View是什么时候运行的。第一个运行的View是BlueViewController,点击其他的tab项时,加载其他的view,加载一次之后下次点击不再调用viewDidLoad。

1.- (void)viewDidLoad

2. [super viewDidLoad]

3. NSLog(@"BlueViewController")

4. // Do any additional setup after loading the view from its nib.

经过自己的测试发现,如果你不在appdelegate中写的,而且appdelegate中用navigationbar作为程序主框架,那么可能会造成冲突。因为navigationbar和tabbar不可能创建两个window放在里面。我就这样写然后造成了tabbar中的子视图中的tableview无法加...


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11518927.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存