UITabbarController 实例一

今天用纯代码来实现UITabbarController的功能,不需要在xib里面使用任何控件 

本文转自 http://www.999dh.net/article/iphone_ios_art/47.html  转载请注明谢谢!
1.建立一个 empty application 工程
2.在 appdelegate.h文件里面实现如下

@interface XYZAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (retain,nonatomic) UITabBarController * tabController;

@end


3.定义3个 viewController   继承自  UIViewController  名字分别为 FirstViewController,SecondViewController,ThirdViewController ,然后分别在 每个view对应的xib文件里面拖上不同的控件(这样做的目的是为了区分在tab 切换的时候已经切换到了不同的view上去)

4.appdeletate.m文件里面实现如下


#import "XYZAppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"

@implementation XYZAppDelegate

@synthesize window = _window;
@synthesize tabController;

- (void)dealloc
{
    [_window 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.
    self.window.backgroundColor = [UIColor whiteColor];
    
    
    FirstViewController * fristView = [[FirstViewController alloc] init];
    SecondViewController* secView = [[SecondViewController alloc] init];
    ThirdViewController * thirdView = [[ThirdViewController alloc] init];
    UITableViewController * forthView = [[UITableViewController alloc] init];
    
    NSArray * array = [[NSArray alloc]initWithObjects:fristView,secView,thirdView, forthView,nil];
    
    tabController = [[UITabBarController alloc]init];
    
    tabController.viewControllers = array;
    
    [[tabController.tabBar.items objectAtIndex:0]setTitle:@"AAA"];
    [[tabController.tabBar.items objectAtIndex:1]setTitle:@"BBBB"];
    [[tabController.tabBar.items objectAtIndex:2]setTitle:@"CCCC"];
    [[tabController.tabBar.items objectAtIndex:3]setTitle:@"DDDD"];
    
    [[tabController.tabBar.items objectAtIndex:0]setImage:[UIImage imageNamed:@"001.png"]];
    [[tabController.tabBar.items objectAtIndex:1]setImage:[UIImage imageNamed:@"002.png"]];
    [[tabController.tabBar.items objectAtIndex:2]setImage:[UIImage imageNamed:@"003.png"]];
    [[tabController.tabBar.items objectAtIndex:3]setImage:[UIImage imageNamed:@"004.png"]];
    
    tabController.selectedIndex = 1;
    
    [self.window addSubview:tabController.view];
    
    
    [array release];
    
    
    [self.window makeKeyAndVisible];
    return YES;
}


实现后的效果如图所示  

原文地址:https://www.cnblogs.com/rollrock/p/2843756.html