TabBarController
2014-01-12 10:05
134 查看
TabBar是控制 控制器的控制器. 跟navigation类似.
在AppDelegate中创建TabBar对象
#import
"AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FouthViewController.h"
#import "FifthViewController.h"
#import "SisthViewController.h"
@implementation AppDelegate
- (void)dealloc
{
[_window
release];
[super
dealloc];
}
@synthesize managedObjectContext =
_managedObjectContext;
@synthesize managedObjectModel =
_managedObjectModel;
@synthesize persistentStoreCoordinator =
_persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window = [[[UIWindow
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]]
autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor
whiteColor];
[self.window
makeKeyAndVisible];
FirstViewController *first = [[[FirstViewController
alloc]
init]autorelease];
SecondViewController *secton = [[[SecondViewController
alloc]
init] autorelease];
ThirdViewController *third = [[[ThirdViewController
alloc]
init] autorelease];
FouthViewController *fouth = [[[FouthViewController
alloc]
init] autorelease];
FifthViewController *fifth = [[[FifthViewController
alloc]
init] autorelease];
SisthViewController *sixth = [[[SisthViewController
alloc]
init] autorelease];
NSArray *array = [NSArray
arrayWithObjects:first,secton,third,fouth,fifth,sixth,
nil];
UITabBarController *tabBar = [[[UITabBarController
alloc]
init] autorelease];
tabBar.viewControllers = array;
tabBar.tabBar.tintColor = [UIColor
greenColor];
tabBar.delegate =
self;
self.window.rootViewController = tabBar;
return
YES;
}
// 以下是代理方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController
*)viewController NS_AVAILABLE_IOS(3_0)
{
NSLog(@"Bool");
return
YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController
*)viewController
{
NSLog(@"didSeclect");
}
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray
*)viewControllers NS_AVAILABLE_IOS(3_0)
{
NSLog(@"1");
}
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray
*)viewControllers changed:(BOOL)changed
NS_AVAILABLE_IOS(3_0)
{
NSLog(@"2");
}
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray
*)viewControllers changed:(BOOL)changed
{
NSLog(@"3");
}
- (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController
NS_AVAILABLE_IOS(7_0)
{
NSLog(@"4");
return
UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController
*)tabBarController NS_AVAILABLE_IOS(7_0)
{
NSLog(@"5");
return
UIInterfaceOrientationPortrait;
}
在第一个试图控制器中设置它的TabBar. (tabBar上的内容有三部分: 图片,文字,标记数字 )
#import
"FirstViewController.h"
@interface
FirstViewController ()
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
UIImage *image = [UIImage
imageNamed:@"searchSelected@2x.png"];
self.tabBarItem.image = image; // 图片
self.tabBarItem.title =
@"第一张"; // 文字
self.tabBarItem.badgeValue =
@"1"; // 徽章个数
}
return
self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
self.view.backgroundColor = [UIColor
randomColor];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
}
在AppDelegate中创建TabBar对象
#import
"AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FouthViewController.h"
#import "FifthViewController.h"
#import "SisthViewController.h"
@implementation AppDelegate
- (void)dealloc
{
[_window
release];
[super
dealloc];
}
@synthesize managedObjectContext =
_managedObjectContext;
@synthesize managedObjectModel =
_managedObjectModel;
@synthesize persistentStoreCoordinator =
_persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window = [[[UIWindow
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]]
autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor
whiteColor];
[self.window
makeKeyAndVisible];
FirstViewController *first = [[[FirstViewController
alloc]
init]autorelease];
SecondViewController *secton = [[[SecondViewController
alloc]
init] autorelease];
ThirdViewController *third = [[[ThirdViewController
alloc]
init] autorelease];
FouthViewController *fouth = [[[FouthViewController
alloc]
init] autorelease];
FifthViewController *fifth = [[[FifthViewController
alloc]
init] autorelease];
SisthViewController *sixth = [[[SisthViewController
alloc]
init] autorelease];
NSArray *array = [NSArray
arrayWithObjects:first,secton,third,fouth,fifth,sixth,
nil];
UITabBarController *tabBar = [[[UITabBarController
alloc]
init] autorelease];
tabBar.viewControllers = array;
tabBar.tabBar.tintColor = [UIColor
greenColor];
tabBar.delegate =
self;
self.window.rootViewController = tabBar;
return
YES;
}
// 以下是代理方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController
*)viewController NS_AVAILABLE_IOS(3_0)
{
NSLog(@"Bool");
return
YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController
*)viewController
{
NSLog(@"didSeclect");
}
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray
*)viewControllers NS_AVAILABLE_IOS(3_0)
{
NSLog(@"1");
}
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray
*)viewControllers changed:(BOOL)changed
NS_AVAILABLE_IOS(3_0)
{
NSLog(@"2");
}
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray
*)viewControllers changed:(BOOL)changed
{
NSLog(@"3");
}
- (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController
NS_AVAILABLE_IOS(7_0)
{
NSLog(@"4");
return
UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController
*)tabBarController NS_AVAILABLE_IOS(7_0)
{
NSLog(@"5");
return
UIInterfaceOrientationPortrait;
}
在第一个试图控制器中设置它的TabBar. (tabBar上的内容有三部分: 图片,文字,标记数字 )
#import
"FirstViewController.h"
@interface
FirstViewController ()
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
UIImage *image = [UIImage
imageNamed:@"searchSelected@2x.png"];
self.tabBarItem.image = image; // 图片
self.tabBarItem.title =
@"第一张"; // 文字
self.tabBarItem.badgeValue =
@"1"; // 徽章个数
}
return
self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
self.view.backgroundColor = [UIColor
randomColor];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
}
相关文章推荐
- 字符串转换整数
- C语言笔记
- java字符串比较获取字符串出现次数的示例
- The Stolen Smell
- c++有感
- gcc源代码分析之gen_call_value()函数
- sh脚本中获取脚本自己的路径
- csdn英雄会(pongo)题解之报数游戏2--源代码
- 初探PHP的SQL注入攻击的技术实现以及预防措施
- csdn英雄会(pongo)题解之报数游戏2
- 多个信号对应单个槽的实现
- java序列化和java反序列化示例
- jquery 的父子节点
- Linux内核 2.4和2.6的进程内核
- 2014新年愿望 -- 开始积累技术博客
- Leetcode: Word Ladder II
- 黑马程序员高级视频学习笔记 将一个系统盘中的文件复制到另一个系统盘中
- DebugView使用详解
- UIView Border color
- 冬天