您的位置:首页 > 产品设计 > UI/UE

UI11_UITabBar

2015-08-13 21:25 351 查看
//
//  AppDelegate.m
//  UI11_UITabBar
//
//  Created by dllo on 15/8/12.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.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]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];

// 创建第一个视图控制器对象
FirstViewController *firstVC = [[FirstViewController alloc] init];
// 创建第一个naVC
UINavigationController *firstNAVC = [[UINavigationController alloc] initWithRootViewController:firstVC];
// 创建tabbar上的按钮及其内容
firstVC.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:1000] autorelease];
firstVC.tabBarItem.badgeValue = @"+99";

// 创建第二个
SecondViewController *secVC = [[SecondViewController alloc] init];
UINavigationController *secNAVC = [[UINavigationController alloc] initWithRootViewController:secVC];
secVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"@一下" image:[UIImage imageNamed:@"2.png"] selectedImage:[UIImage imageNamed:@"7.png"]] autorelease];

// 创建第三个
ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
UINavigationController *thirdNAVC = [[UINavigationController alloc] initWithRootViewController:thirdVC];
thirdVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"设置" image:[UIImage imageNamed:@"6.png"] tag:1001] autorelease];

// 创建第四个
FourthViewController *fourthVC = [[FourthViewController alloc] init];
UINavigationController *fourthNAVC = [[UINavigationController alloc] initWithRootViewController:fourthVC];
fourthVC.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1002] autorelease];

FifthViewController *fifthVC = [[FifthViewController alloc] init];
UINavigationController *fifthNAVC = [[UINavigationController alloc] initWithRootViewController:fifthVC];
fifthVC.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1003] autorelease];

SixthViewController *sixthVC = [[SixthViewController alloc] init];
UINavigationController *sixthNAVC = [[UINavigationController alloc] initWithRootViewController:sixthVC];
sixthVC.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:1004] autorelease];

#pragma mark 按钮创建好,然后创建一个UITabBarController 让所有的按钮显示出来
UITabBarController *tabVC = [[UITabBarController alloc] init];
// tabbarController通过一个数组来管理所有要显示出来的naVC
tabVC.viewControllers = @[firstNAVC, secNAVC, thirdNAVC, fourthNAVC, fifthNAVC, sixthNAVC];
self.window.rootViewController = tabVC;

// 对tabbar进行外观设置
tabVC.tabBar.translucent = NO;
// tabbar的背景颜色
tabVC.tabBar.barTintColor = [UIColor greenColor];
// 按钮点击之后的颜色
tabVC.tabBar.tintColor = [UIColor colorWithRed:255 / 255.0 green:212 / 255.0 blue:0  alpha:1];

// 设置代理人
tabVC.delegate = self;
// 默认选择从第几个开始
//    tabVC.selectedIndex = 0;

// 释放
[tabVC release];
[sixthNAVC release];
[sixthVC release];
[fifthNAVC release];
[fifthVC release];
[fourthNAVC release];
[fourthVC release];
[thirdNAVC release];
[thirdVC release];
[secNAVC release];
[secVC release];
[firstNAVC release];
[firstVC release];

return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// 点击之后变成红点
//    viewController.tabBarItem.badgeValue = @"";
// 设置为空
viewController.tabBarItem.badgeValue = nil;
}

- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
//
//  FirstViewController.m
//  UI11_UITabBar
//
//  Created by dllo on 15/8/12.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic ,retain)NSMutableArray *arr;
@property(nonatomic, retain)UITableView *tableView;
@end

@implementation FirstViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
}
return self;
}

- (void)dealloc
{
[_arr release];
[_tableView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO;

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
// 当没有cell的时候创建一个cell的同时也创建手势和控件等,当cell存在时,就不会在执行if语句,在if语句中添加手势和控件,能够实现手势或者控件的重用.如果在if语句外面创建手势和控件意味着每一次执行的时候都要重新创建一个手势和控件,有几个cell就创建几个手势或控件
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
[cell addGestureRecognizer:longPress];
[longPress release];
//
// 添加控件,button的重用,但是如果写在if语句外面则无法实现重用,可以在自定义cell中添加
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//        [button setTitle:@"测试" forState:UIControlStateNormal];
//        [cell addSubview:button];
//        button.frame = CGRectMake(300, 0, 100, 40);
}
// 每一个cell就是一行
// 将手势控件等放在if外面,有多少个cell就会创建多少个手势,控件,放在if 内部实现重用,跟随cell的创建创建手势和控件,cell已有,则跟随cell重用
cell.textLabel.text = self.arr[indexPath.row];
return cell;
}

- (void)click:(UILongPressGestureRecognizer *)longPress
{
// 通过手势,找到手势所添加的cell
UITableViewCell *cell = (UITableViewCell *)longPress.view;

// 创建一个快捷菜单,单例
UIMenuController *menu = [UIMenuController sharedMenuController];

// 给这个快捷菜单进行定位
[menu setTargetRect:cell.frame inView:cell.superview];
// 让菜单显示出来
[menu setMenuVisible:YES animated:YES];

// 如果想使用自定义的功能
UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"测试" action:@selector(flag)];

// 把这个按钮放到快捷菜单上
[menu setMenuItems:@[flag]];
// 按钮的方法必须要实现,无论是系统的还是自定义的,如果不实现对应的方法,不会添加到快捷菜单上

}

#pragma mark 快捷菜单捆绑了一个方法,这个方法必须实现,如果不实现,快捷菜单没有办法显示
- (BOOL)canBecomeFirstResponder
{
return YES;
}

- (void)flag
{
NSLog(@"2222222");
}

- (void)delete:(id)sender
{
NSLog(@"删除");
}

- (void)copy:(id)sender
{
NSLog(@"复制");
}

- (void)paste:(id)sender
{
NSLog( @"粘贴");
}

- (void)select:(id)sender
{
NSLog(@"选择");
}

- (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


//
//  SecondViewController.m
//  UI11_UITabBar
//
//  Created by dllo on 15/8/12.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
}

- (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


//
//  ThirdViewController.m
//  UI11_UITabBar
//
//  Created by dllo on 15/8/12.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "ThirdViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blueColor];
}

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