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;
}
#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];
}