#import "AppDelegate.h"
#import "RootTabBarController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RootTabBarController *root = [RootTabBarController new];
self.window.rootViewController = root;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
#import "RootTabBarController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "CustomTabBarView.h"
@interface RootTabBarController ()<CustomTabBarViewDelegate>
@end
@implementation RootTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
OneViewController *oneVC = [OneViewController new];
UINavigationController *oneNavC = [[UINavigationControlleralloc]initWithRootViewController:oneVC];
TwoViewController *twoVC = [TwoViewController new];
UINavigationController *twoNavC = [[UINavigationControlleralloc]initWithRootViewController:twoVC];
//給tabVarController添加子控制器(等同於:將標籤加入到控制器中)
[self addChildViewController:oneNavC];
[self addChildViewController:twoNavC];
//自定義
//如果使用自定義的tabBar,可以先將系統的隱藏掉,然後用自己的
self.tabBar.hidden = YES;
//初始化自定義的tabBar
CustomTabBarView *customView = [[CustomTabBarViewalloc]initWithFrame:CGRectZero];
//指定代理
customView.delegate = self;
[self.view addSubview:customView];
self.view.backgroundColor = [UIColor yellowColor];
}
//實現代理方法
-(void)selectIndexWithTag:(int)tag{
self.selectedIndex = tag;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import "OneViewController.h"
@interface OneViewController ()
@end
@implementation OneViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"One";
self.view.backgroundColor = [UIColor cyanColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import "TwoViewController.h"
@interface TwoViewController ()
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Two";
self.view.backgroundColor = [UIColor grayColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import "CustomTabBarView.h"
@implementation CustomTabBarView
//初始化界面元素
-(void)createSubViews{
for (int i = 0; i<2; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:[@(i) stringValue] forState:UIControlStateNormal];
[btn setTitle:@"MLGBWC" forState:UIControlStateSelected];
//通過tag值來確定我們點擊的是哪個按鈕,來切換到對應的視圖控制器
[btn setTag:1000+i];
[btn addTarget:self action:@selector(customSelectorIndex:) forControlEvents:UIControlEventTouchDown];
//設置按鈕的位置
btn.frame = CGRectMake((kScreenWidth/2*i), 0, kScreenWidth/2, 50);
//添加按鈕
[self addSubview:btn];
}
}
//按鈕的回調方法
-(void)customSelectorIndex:(UIButton *)sender{
//這個方法的作用是:判斷在指定的代理類中是否實現了該協議方法,如果實現,返回值爲YES,如果未實現該協議方法,返回值爲NO,這樣就保證了執行協議方法的時候不會崩潰。
if ([self.delegate respondsToSelector:@selector(selectIndexWithTag:)]) {
[self.delegate selectIndexWithTag:(int)sender.tag-1000];
}else{
NSLog(@"selectIndexWithTag--該方法未實現");
}
//給按鈕添加選中狀態
for (int i = 0 ; i<2; i++) {
UIButton *btn = (UIButton *)[self viewWithTag:1000+i];
btn.selected = NO;
}
sender.selected = YES;
NSLog(@"按鈕回調方法");
}
//寫初始化方法
-(instancetype)initWithFrame:(CGRect)frame{
//重新設置frame,對它進行一個保護
frame = CGRectMake(0, kScreenHeight-50, kScreenWidth, 50);
self = [super initWithFrame:frame];
if (self) {
[self createSubViews];
//更改顏色
self.backgroundColor = [UIColor yellowColor];
}
return self;
}