该教程为自己记录使用,要求读者至少有一点点简单的Xcode开发经验即可
Demo功能介绍:
下图所示即可知道,TabbarController是控制器,控制着五个页面的
Demo创建过程:
首先是文件的一些说明:
TabbarController是UITabBarController的实现
FirstViewController是UIViewController的实现
SecondViewController是UIViewController的实现
ThirdViewController是UIViewController的实现
ForthViewController是UIViewController的实现
FifthViewController是UIViewController的实现
1.首先创建项目——(SingleViewApp)
2.创建TabbarController,继承于UITabBarController
3.创建FirstViewController,SecondViewController,ThirdViewController,ForthViewController以及FifthViewController,它们都是UIViewController的实现,同时创建的时候要连同
xib文件一起创建(方便以后的布局)
3.在主页面创建一个button,添加点击事件代码如下:
- (IBAction)goTabbarController:(id)sender {
TabBarController *tabbarController=[[TabBarController alloc] init];
[tabbarController setTitle:@"TabBarController"];
[self presentModalViewController:tabbarController animated:YES];
}
这样,点击事件后就跳转到TabbarController中
4.接下来,连接TabbarController和五个ViewController,
TabBarController.h文件如下:
(注意!!我们要自定义Tabbar样式,舍弃系统自定义格式,思路是将原有的Tabbar隐藏,然后创建一个View作为TabbarItem的承载体,然后还要创建五个UIButton)
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "ForthViewController.h"
#import "FifthViewController.h"
@interface TabBarController : UITabBarController{
UIView *tabBarView;
UIButton *firstButton;
UIButton *secondButton;
UIButton *thirdButton;
UIButton *forthButton;
UIButton *fifthButton;
}
@property (retain, nonatomic) UIView *tabBarView;
@property (retain, nonatomic) UIButton *firstButton;
@property (retain, nonatomic) UIButton *secondButton;
@property (retain, nonatomic) UIButton *thirdButton;
@property (retain, nonatomic) UIButton *forthButton;
@property (retain, nonatomic) UIButton *fifthButton;
@end
5.下面是 TabBarController.m文件
#import "TabBarController.h"
@interface TabBarController ()
@end
@implementation TabBarController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化TabBarController,将TabBarController与五个ViewController关联起来
[self initTabBarController];
//初始化自定义TabBarView的界面
[self initTabBarView];
}
-(void) initTabBarController{
NSMutableArray *items = [[NSMutableArray alloc] init];
FirstViewController *firstView = [[FirstViewController alloc] init];
[items addObject:firstView];
//这一句话是对firstView的监听,实现从第一个view跳转到第二个view
firstView.delegate=self;
SecondViewController *secondView = [[SecondViewController alloc] init];
[items addObject:secondView];
ThirdViewController *thirdView=[[ThirdViewController alloc] init];
[items addObject:thirdView];
ForthViewController *forthView=[[ForthViewController alloc] init];
[items addObject:forthView];
FifthViewController *fifthView=[[FifthViewController alloc] init];
[items addObject:fifthView];
[self setViewControllers:items];
}
-(void) initTabBarView{
CGRect rx = [UIScreen mainScreen].bounds;
int screenHeight=(int)rx.size.height;
//针对不同屏幕尺寸,调整TabBarView的高度
if(screenHeight==568){
tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 568 - 50, 320, 50)];//49为tabBar的高度
} else {
tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 480 - 50, 320, 50)];//49为tabBar的高度
}
tabBarView.backgroundColor = [UIColor grayColor];//设置tabBar的背景颜色
[self.view addSubview:tabBarView];
firstButton=[UIButton buttonWithType:UIButtonTypeCustom];
secondButton=[UIButton buttonWithType:UIButtonTypeCustom];;
thirdButton=[UIButton buttonWithType:UIButtonTypeCustom];;
forthButton=[UIButton buttonWithType:UIButtonTypeCustom];;
fifthButton=[UIButton buttonWithType:UIButtonTypeCustom];
//初始化button样式
[self initTabButton:firstButton normalImage:@"icon001.png" buttonTag:0];
[self initTabButton:secondButton normalImage:@"icon002.png" buttonTag:1];
[self initTabButton:thirdButton normalImage:@"icon003.png" buttonTag:2];
[self initTabButton:forthButton normalImage:@"icon004.png" buttonTag:3];
[self initTabButton:fifthButton normalImage:@"icon005.png" buttonTag:4];
//将Button添加到TabBarView上
[tabBarView addSubview:firstButton];
[tabBarView addSubview:secondButton];
[tabBarView addSubview:thirdButton];
[tabBarView addSubview:forthButton];
[tabBarView addSubview:fifthButton];
}
-(void) initTabButton: (UIButton *)button normalImage:(NSString *)normalImage buttonTag:(int) i{
button.frame = CGRectMake((64-30)/2+(i*64), (49-30)/2, 30, 30);
button.tag = i;
[button setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
[button addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
}
//TabBar上Button点击时对应的方法
- (void)selectedTab:(UIButton *)button {
self.selectedIndex = button.tag;
}
//实现FirstViewDelegate方法
-(void) goToSecondView{
//跳转到第二个View
[self setSelectedIndex:1];
}
-(void) backToMain{
//返回主页面
[self dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
6. 开始介绍FirstViewController的界面,主要就是添加一个返回主界面的按钮,以及一个跳转到第二个View的按钮
FirstViewController.h:
#import <UIKit/UIKit.h>
@class FirstViewController;
@protocol FirstViewControllerDelegate<NSObject>
@required
-(void) goToSecondView;
-(void) backToMain;
@end
@interface FirstViewController : UIViewController{
id<FirstViewControllerDelegate> delegate;
}
@property (retain, nonatomic) id<FirstViewControllerDelegate> delegate;
@property (retain, nonatomic) IBOutlet UIButton *backButton;
@property (retain, nonatomic) IBOutlet UIButton *goSecondView;
- (IBAction)goBackMain:(id)sender;
- (IBAction)goSecondView:(id)sender;
@end
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)goBackMain:(id)sender {
[self.delegate backToMain];
}
- (IBAction)goSecondView:(id)sender {
[self.delegate goToSecondView];
}
@end
7. 在第二个页面添加tableview和导航栏
其实道理很简单,就是先创建一个UINavigatorController A,然后关联UIViewController B(使用pushViewController方法),将A作为子元素添加到TabBar的items中即可,如下
SettingView_iPhone *settingView=[[SettingView_iPhone alloc] init];
settingNavController=[[UINavigationController alloc]init];
[settingNavController pushViewController:settingView animated:NO];
[items addObject:settingNavController];
其中,settingNavController是UINavigatorController.