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

UITabBarController使用详解

2015-11-27 23:24 579 查看
UITabBarController是IOS中很常用的一个viewController,例如系统的闹钟程序,ipod 程序等。UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的container viewController中。

  首先我们看一下它的view层级图:

//
//  SvTabBarAppDelegate.m
//  SvTabBarControllerSample
//
//  Created by maple on 5/19/12.
//  Copyright (c) 2012 smileEvday. All rights reserved.
//

#import "SvTabBarAppDelegate.h"

#import "SvTabBarFirstViewController.h"
#import "SvTabBarSecondViewController.h"

@implementation SvTabBarAppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (void)dealloc
{
[_window release];
[_tabBarController release];
[super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
SvTabBarFirstViewController *viewController1, *viewController2, *viewController3;
SvTabBarFirstViewController *viewController4, *viewController5, *viewController6;

// second kind tabbar controlle
SvTabBarSecondViewController *viewController7;

// init first tabbarviewcontroller
viewController1 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil];
viewController1.title = @"First";
// here set text is in vain, because the vc's view is not load
//viewController1.contentLbl.text = @"First Tab";

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 150, 70);
btn.center = CGPointMake(160, 120);
btn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[btn setTitle:@"Change to Third" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(changToThirdTab) forControlEvents:UIControlEventTouchUpInside];
[viewController1.view addSubview:btn];

btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 150, 70);
btn.center = CGPointMake(160, 240);
btn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[btn setTitle:@"Change to MoreVC" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(changToMoreTab) forControlEvents:UIControlEventTouchUpInside];
[viewController1.view addSubview:btn];

btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 150, 70);
btn.center = CGPointMake(160, 360);
btn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[btn setTitle:@"Set ViewController" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(setTabBarViewController) forControlEvents:UIControlEventTouchUpInside];
[viewController1.view addSubview:btn];

viewController2 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil];
viewController2.title = @"Second";
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 150, 70);
btn.center = CGPointMake(160, 120);
btn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[btn setTitle:@"Change to Fourth" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(changToFourthTab) forControlEvents:UIControlEventTouchUpInside];
[viewController2.view addSubview:btn];

btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 150, 70);
btn.center = CGPointMake(160, 240);
btn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[btn setTitle:@"Set Badge" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(setBadge) forControlEvents:UIControlEventTouchUpInside];
[viewController2.view addSubview:btn];

viewController3 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil];
viewController3.title = @"Third";
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"#3#" image:nil tag:2];
[item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"]
withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];
viewController3.tabBarItem = item;
[item release];

viewController4 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil];
viewController4.title = @"Fourth";

viewController5 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil];
viewController5.title = @"Fifth";

viewController6 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil];
viewController6.title = @"Sixth";

// only support portrait interface, so when you add this vc to tabBarcontroller,
// the tabbarcontroller will support portrait interface only
viewController7 = [[SvTabBarSecondViewController alloc] initWithNibName:nil bundle:nil];

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.delegate = self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3,
viewController4, viewController5, viewController6, viewController7, nil];
self.tabBarController.customizableViewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3,
viewController4, nil];

[viewController1 release];
[viewController2 release];
[viewController3 release];
[viewController4 release];
[viewController5 release];
[viewController6 release];
[viewController7 release];

self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

// Directly modifying a tab bar managed by a tab bar controller is not allowed.
//    self.tabBarController.tabBar.selectedItem = nil;

return YES;
}

- (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:.
}

/*
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
}
*/

/*
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
}
*/

#pragma mark -
#pragma mark Btn action

- (void)changToThirdTab
{
[self.tabBarController setSelectedViewController:[self.tabBarController.viewControllers objectAtIndex:2]];
}

- (void)changToFourthTab
{
[self.tabBarController setSelectedIndex:3];
}

// set tabbar viewcontrollers will lead customizableViewControllers set to default.(equal to viewcontrollers)
// so if you want to restrict customizableViewControllers, you should reset it when viewcontrollers changed
- (void)setTabBarViewController
{
self.tabBarController.viewControllers = self.tabBarController.viewControllers;
}

// you can just select more tab by set selectedViewController, set selectedIndex is in vain
- (void)changToMoreTab
{
[self.tabBarController setSelectedViewController:self.tabBarController.moreNavigationController];

// when set more tab selected, the selectedindex will be NSNotFound
NSLog(@"%d", self.tabBarController.selectedIndex);
}

- (void)setBadge
{
self.tabBarController.selectedViewController.tabBarItem.badgeValue = @"2";
}

#pragma mark-
#pragma mark UITabBarcontrollerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
{
NSLog(@"willBeginCustomizingViewControllers: %@", viewControllers);
}

- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
NSLog(@"viewcontrollers: %@, ischanged: %d", viewControllers, changed);
}

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
NSLog(@"didEndCustomizingViewController!");
NSLog(@"didEndCustomizingViewController: %@, ischanged: %d", viewControllers, changed);
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if ([tabBarController.viewControllers indexOfObject:viewController] != 2) {
return YES;
}
return NO;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"didSelectViewController!");
}

@end


//
//  SvTabBarFirstViewController.h
//  SvTabBarControllerSample
//
//  Created by maple on 5/19/12.
//  Copyright (c) 2012 smileEvday. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SvTabBarFirstViewController : UIViewController

@end


//
//  SvTabBarFirstViewController.m
//  SvTabBarControllerSample
//
//  Created by maple on 5/19/12.
//  Copyright (c) 2012 smileEvday. All rights reserved.
//

#import "SvTabBarFirstViewController.h"

@interface SvTabBarFirstViewController ()

@end

@implementation SvTabBarFirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

self.tabBarItem.image = [UIImage imageNamed:@"first"];
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"%@ rotate!", self.title);
}

- (NSString*)description
{
return [NSString stringWithFormat:@"title: %@", self.title];
}

@end


//
//  SvTabBarSecondViewController.h
//  SvTabBarControllerSample
//
//  Created by maple on 5/19/12.
//  Copyright (c) 2012 smileEvday. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SvTabBarSecondViewController : UIViewController

@end


//
//  SvTabBarSecondViewController.m
//  SvTabBarControllerSample
//
//  Created by maple on 5/19/12.
//  Copyright (c) 2012 smileEvday. All rights reserved.
//

#import "SvTabBarSecondViewController.h"

@interface SvTabBarSecondViewController ()

@end

@implementation SvTabBarSecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.image = [UIImage imageNamed:@"second"];
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor greenColor];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}

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