纯代码定义一个标准的tabBar

本文详细介绍了如何使用Objective-C编程语言创建一个带有Tab Bar Controller的iOS应用,包括Tab Bar Controller的基本概念、如何实现Tab Bar Controller、以及如何在Tab Bar Controller中添加不同类型的视图控制器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AppDelegate.h 头文件

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UITabBarController * tabBar;

}

@property(nonatomic,retain)UITabBarController * tabBar;

@property (strong, nonatomic) UIWindow *window;

@end


AppDelegate.m

#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
#import "FiveViewController.h"
@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBar;//tabBar接口
- (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 * first = [[[FirstViewController alloc]init]autorelease];	//实例化视图控制器
    SecondViewController * second = [[[SecondViewController alloc]init]autorelease];
    ThreeViewController * three = [[[ThreeViewController alloc]init]autorelease];
    FourViewController * four =  [[[FourViewController alloc]init]autorelease];
    FiveViewController * five = [[[FiveViewController alloc]init]autorelease];
    
    UINavigationController *firstNavController =[[UINavigationController alloc] initWithRootViewController:first];	//实例化导航控制器,并关联视图控制器
    UINavigationController *secondNavController =[[UINavigationController alloc] initWithRootViewController:second];
    UINavigationController *threeNavController = [[UINavigationController alloc] initWithRootViewController:three];
    UINavigationController *fourNavController = [[UINavigationController alloc] initWithRootViewController:four];
    UINavigationController *fiveNacController = [[UINavigationController alloc] initWithRootViewController:five];
    
    NSArray * viewControllers = [NSArray arrayWithObjects:firstNavController,secondNavController,threeNavController,fourNavController,fiveNacController,nil];	//创建一个静态数组,并关联导航控制器
    
    tabBar = [[UITabBarController alloc]init];	//为tabBar分配空间,并初始化
    tabBar.viewControllers = viewControllers;	//将导航控制器数组关联到tabBar
    [self.window addSubview:tabBar.view];	//添加tabBar视图到window
      
//=================================================     
    [self.window makeKeyAndVisible];
    return YES;
}

@end

一个视图控制器FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
{
    //....................
}

@end


在FirstViewController.m中对tabBar进行设置

FirstViewController.m

#import "FirstViewController.h"
@interface FirstViewController()

@end

@implementation FirstViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.tabBarItem.image = [UIImage imageNamed:@"tabBar底部小图标.png"];
        self.tabBarItem.title = @"小图标底部小字";
        self.navigationItem.title = @"导航栏标题";
    }
    return self;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值