您的位置:首页 > 移动开发 > Objective-C

Objective - C UITabbarController

2015-12-24 21:26 531 查看
//

// RootViewController.m

// UI_12day_UITabbarController

//

// Created by dllo on 15/12/23.

// Copyright © 2015年 dllo. All rights reserved.

//

#import "RootViewController.h"

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *arr;

@property(nonatomic,retain)UITableView *tableView;

@end

@implementation RootViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor cyanColor];

self.title = @"第一页";

self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松", @"徐宁", @"张清", @"杨志", @"董平", @"索超", @"戴宗", @"刘唐", @"李逵", @"史进", @"穆弘", @"雷横", @"李俊",nil];

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain];

self.tableView.dataSource = self;

self.tableView.delegate = self;

[self.view addSubview:self.tableView];

[_tableView release];

// 导航视图不透明

self.navigationController.navigationBar.translucent = NO;

// 另一个Cell方式

// tableView的对cell的注册

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.arr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

// 新的Cell方式:用系统的Cell无法加视图,必须通过自定义Cell使用

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];

cell.textLabel.text = self.arr[indexPath.row];

return cell;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

///////////////////////////////////////////


AppDelegate.m

#import "AppDelegate.h"

#import "RootViewController.h"

#import "SecondViewController.h"

#import "ThirdViewController.h"

#import "FouthViewController.h"

#import "FifthViewController.h"

#import "SixthViewController.h"

// 签协议

@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate

- (void)dealloc

{

[_window release];

[super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

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

self.window.backgroundColor =
[UIColor whiteColor];

[self.window makeKeyAndVisible];

[_window release];

RootViewController *rootVC = [[RootViewController alloc] init];

// navigation用来管理viewController

UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];

// tabbarController来管理navigation

// 创建一个tabbar的按钮内容,一个按钮对应一个navigation

naVC.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1000] autorelease];

// 显示消息计数

naVC.tabBarItem.badgeValue = @"+99";

// 创建第二个naVC

SecondViewController *secVC = [[SecondViewController alloc] init];

UINavigationController *secNAVC = [[UINavigationController alloc] initWithRootViewController:secVC];

secNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"contect" image:
[UIImage imageNamed:@"name.png"]tag:1001];

// 创建第三个naVC

ThirdViewController *thirdVC = [[ThirdViewController alloc] init];

UINavigationController *thirdNAVC = [[UINavigationController alloc] initWithRootViewController:thirdVC];

thirdNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"定位" image:[UIImage imageNamed:@"dingwei.png"] selectedImage:[UIImage imageNamed:@"time.png"]];

// 创建第四个

FouthViewController *fouthVC = [[FouthViewController alloc] init];

UINavigationController *fouthNAVC = [[UINavigationController alloc] initWithRootViewController:fouthVC];

fouthNAVC.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1003] autorelease];

// 创建第五个

FifthViewController *fifVC = [[FifthViewController alloc] init];

UINavigationController *fifthNAVC = [[UINavigationController alloc] initWithRootViewController:fifVC];

fifthNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"时间" image:[UIImage imageNamed:@"time.png"] tag:1004];

// 创建第六个

SixthViewController *sixVC = [[SixthViewController alloc] init];

UINavigationController *sixNAVC = [[UINavigationController alloc] initWithRootViewController:sixVC];

sixNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"通知" image:[UIImage imageNamed:@"1.jpg"] selectedImage:[UIImage imageNamed:@"2.jpg"]];

// 创建tabbarController

UITabBarController *tabVC = [[UITabBarController alloc] init];

// 设置tabVC管理的naVC

tabVC.viewControllers = @[naVC,secNAVC,thirdNAVC,fouthNAVC,fifthNAVC,sixNAVC];

// 设置window的根视图控制器

self.window.rootViewController =
tabVC;

// 设置代理人

tabVC.delegate = self;

[tabVC release];

[naVC release];

[rootVC release];

[secNAVC release];

[secVC release];

[thirdNAVC release];

[thirdVC release];

[fouthNAVC release];

[fouthVC release];

[fifthNAVC release];

[fifVC release];

[sixNAVC release];

[sixVC release];

// 设置一下tabbar的外观

// 修改点击后图标颜色,默认点击为蓝色

tabVC.tabBar.translucent = NO;

tabVC.tabBar.tintColor = [UIColor brownColor];

// 修改bar背景颜色

tabVC.tabBar.barTintColor = [UIColor cyanColor];

return YES;

}

#pragma mark 这个方法相当于tab的点击方法

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

NSLog(@"14111");

// 通过点击,把显示消息的数字取消

viewController.tabBarItem.badgeValue = nil;

}


//

// RootViewController.m

// UI_12day_UITabbarController

//

// Created by dllo on 15/12/23.

// Copyright © 2015年 dllo. All rights reserved.

//

#import "RootViewController.h"

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *arr;

@property(nonatomic,retain)UITableView *tableView;

@end

@implementation RootViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor cyanColor];

self.title = @"第一页";

self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松", @"徐宁", @"张清", @"杨志", @"董平", @"索超", @"戴宗", @"刘唐", @"李逵", @"史进", @"穆弘", @"雷横", @"李俊",nil];

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain];

self.tableView.dataSource = self;

self.tableView.delegate = self;

[self.view addSubview:self.tableView];

[_tableView release];

// 导航视图不透明

self.navigationController.navigationBar.translucent = NO;

// 另一个Cell方式

// tableView的对cell的注册

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.arr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

// 新的Cell方式:用系统的Cell无法加视图,必须通过自定义Cell使用

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];

cell.textLabel.text = self.arr[indexPath.row];

return cell;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

///////////////////////////////////////////


AppDelegate.m

#import "AppDelegate.h"

#import "RootViewController.h"

#import "SecondViewController.h"

#import "ThirdViewController.h"

#import "FouthViewController.h"

#import "FifthViewController.h"

#import "SixthViewController.h"

// 签协议

@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate

- (void)dealloc

{

[_window release];

[super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

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

self.window.backgroundColor =
[UIColor whiteColor];

[self.window makeKeyAndVisible];

[_window release];

RootViewController *rootVC = [[RootViewController alloc] init];

// navigation用来管理viewController

UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];

// tabbarController来管理navigation

// 创建一个tabbar的按钮内容,一个按钮对应一个navigation

naVC.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1000] autorelease];

// 显示消息计数

naVC.tabBarItem.badgeValue = @"+99";

// 创建第二个naVC

SecondViewController *secVC = [[SecondViewController alloc] init];

UINavigationController *secNAVC = [[UINavigationController alloc] initWithRootViewController:secVC];

secNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"contect" image:
[UIImage imageNamed:@"name.png"]tag:1001];

// 创建第三个naVC

ThirdViewController *thirdVC = [[ThirdViewController alloc] init];

UINavigationController *thirdNAVC = [[UINavigationController alloc] initWithRootViewController:thirdVC];

thirdNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"定位" image:[UIImage imageNamed:@"dingwei.png"] selectedImage:[UIImage imageNamed:@"time.png"]];

// 创建第四个

FouthViewController *fouthVC = [[FouthViewController alloc] init];

UINavigationController *fouthNAVC = [[UINavigationController alloc] initWithRootViewController:fouthVC];

fouthNAVC.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1003] autorelease];

// 创建第五个

FifthViewController *fifVC = [[FifthViewController alloc] init];

UINavigationController *fifthNAVC = [[UINavigationController alloc] initWithRootViewController:fifVC];

fifthNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"时间" image:[UIImage imageNamed:@"time.png"] tag:1004];

// 创建第六个

SixthViewController *sixVC = [[SixthViewController alloc] init];

UINavigationController *sixNAVC = [[UINavigationController alloc] initWithRootViewController:sixVC];

sixNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"通知" image:[UIImage imageNamed:@"1.jpg"] selectedImage:[UIImage imageNamed:@"2.jpg"]];

// 创建tabbarController

UITabBarController *tabVC = [[UITabBarController alloc] init];

// 设置tabVC管理的naVC

tabVC.viewControllers = @[naVC,secNAVC,thirdNAVC,fouthNAVC,fifthNAVC,sixNAVC];

// 设置window的根视图控制器

self.window.rootViewController =
tabVC;

// 设置代理人

tabVC.delegate = self;

[tabVC release];

[naVC release];

[rootVC release];

[secNAVC release];

[secVC release];

[thirdNAVC release];

[thirdVC release];

[fouthNAVC release];

[fouthVC release];

[fifthNAVC release];

[fifVC release];

[sixNAVC release];

[sixVC release];

// 设置一下tabbar的外观

// 修改点击后图标颜色,默认点击为蓝色

tabVC.tabBar.translucent = NO;

tabVC.tabBar.tintColor = [UIColor brownColor];

// 修改bar背景颜色

tabVC.tabBar.barTintColor = [UIColor cyanColor];

return YES;

}

#pragma mark 这个方法相当于tab的点击方法

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

NSLog(@"14111");

// 通过点击,把显示消息的数字取消

viewController.tabBarItem.badgeValue = nil;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: