iPhone开发多视图技术总结之二:Tab Bar .

实现的功能:通过Tab Bar,实现多视图切换。

关键词:多视图 Tab Bar


1、创建一个Empty Application工程,命名为:MultiView-Tab,如下图

[img]
[img]http://dl.iteye.com/upload/attachment/0078/7272/575bc886-66dd-3919-9991-bc925d42aaf8.png[/img]
[/img]


2、选中工程中的Group MultiView-Tab,然后按住CMD(Windows键)+N,新建视图控制器FirstViewController,如下图
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7274/33df56f8-5739-357d-a2a0-5042adbb345a.png[/img]
[/img]


3、依照上步操作,新建视图控制器SecondViewController


4、编辑FirstViewController.xib,在Bottom Bar显示出Tab Bar,然后添加一个Lable,如下图
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7276/3af7ef84-ff4b-37a5-beb9-09627710bc6c.png[/img]
[/img]

[img]
[img]http://dl.iteye.com/upload/attachment/0078/7278/eb43ae9b-4ae9-348c-8769-8c66b52b3082.png[/img]
[/img]


5、依照上部操作,设置SecondViewController.xib


6、新建Group,名称为:Images,添加4张png图片
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7280/bef7f58f-59ae-3399-b3fb-9339fc08ff85.png[/img]
[/img]


7、万事俱备,开始写代码,首先修改AppDelegate,添加一个UITabBarController类的实例,作为根视图控制器用。

AppDelegate.h修改后如下:
[img]#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
//添加代码
@property(strong,nonatomic) UITabBarController *tabBarController;
@end[/img]

AppDelegate.m主要修改didFinishLaunchingWithOptions方法,修改后如下:
#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
//添加代码
@synthesize tabBarController;
- (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.
//添加代码

//初始化firstViewController
UIViewController *firstViewController = [[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil] autorelease];
//初始化secondViewController
UIViewController *secondViewController = [[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil] autorelease];

self.tabBarController = [[[UITabBarController alloc]init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:firstViewController,secondViewController, nil];

//设置tabBarController为根视图控制器
self.window.rootViewController = tabBarController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
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:.
}

@end



8、修改FirstViewController.m,初始化时设置tabBarItem的title和显示的图片,仅修改方法initWithNibName,如下:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title = @"First View";
self.tabBarItem.image = [UIImage imageNamed:@"first"];
}
return self;
}



9、依照上步修改SecondViewController.m,如下:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title = @"Second View";
self.tabBarItem.image = [UIImage imageNamed:@"second"];
}
return self;
}



10、编译、运行,效果如下
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7282/8321ffeb-b2e5-361c-bba4-c8e2af4f380f.png[/img]
[/img]


总结:1)本文通过手工编写代码模拟了通过Tab Bar实现多视图切换;

2)Xcode提供了Tabbed Application模板,如下

[img]
[img]http://dl.iteye.com/upload/attachment/0078/7284/7d82f60d-c8a1-36fc-8be5-67d1d229f73d.png[/img]
[/img]
基于该模板创建的工程就是Tab Bar实现的多视图应用程序,与本文实现的功能相同。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值