iPhone开发【九】多视图技术总结之二:Tab Bar
发布日期:2021-09-28 18:46:14 浏览次数:10 分类:技术文章

本文共 4647 字,大约阅读时间需要 15 分钟。

转载请注明出处,原文网址: 作者:张燕广

最近很忙,但是仍然想坚持写完想写的内容,坚持!

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

关键词:多视图 Tab Bar

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

2、选中工程中的Group MultiView-Tab,然后按住CMD(Windows键)+N,新建视图控制器FirstViewController,如下图

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

4、编辑FirstViewController.xib,在Bottom Bar显示出Tab Bar,然后添加一个Lable,如下图

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

6、新建Group,名称为:Images,添加4张png图片

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

AppDelegate.h修改后如下:

[cpp] 
  1. //  
  2. //  AppDelegate.h  
  3. //  MultiView-Tab  
  4. //  
  5. //  Created by Zhang Yanguang on 12-11-20.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #import "FirstViewController.h"  
  11. #import "SecondViewController.h"  
  12. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  13.   
  14. @property (strong, nonatomic) UIWindow *window;  
  15. //添加代码  
  16. @property(strong,nonatomic) UITabBarController *tabBarController;  
  17. @end  
AppDelegate
.m主要修改didFinishLaunchingWithOptions方法,修改后如下:

[cpp] 
  1. //  
  2. //  AppDelegate.m  
  3. //  MultiView-Tab  
  4. //  
  5. //  Created by Zhang Yanguang on 12-11-20.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import "AppDelegate.h"  
  10.   
  11. @implementation AppDelegate  
  12.   
  13. @synthesize window = _window;  
  14. //添加代码  
  15. @synthesize tabBarController;  
  16. - (void)dealloc  
  17. {  
  18.     [_window release];  
  19.     [super dealloc];  
  20. }  
  21.   
  22. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  23. {  
  24.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  25.     // Override point for customization after application launch.  
  26.     //添加代码  
  27.       
  28.     //初始化firstViewController  
  29.     UIViewController *firstViewController = [[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil] autorelease];  
  30.     //初始化secondViewController  
  31.     UIViewController *secondViewController = [[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil] autorelease];  
  32.       
  33.     self.tabBarController = [[[UITabBarController alloc]init] autorelease];  
  34.     self.tabBarController.viewControllers = [NSArray arrayWithObjects:firstViewController,secondViewController, nil];  
  35.       
  36.     //设置tabBarController为根视图控制器  
  37.     self.window.rootViewController = tabBarController;  
  38.     self.window.backgroundColor = [UIColor whiteColor];  
  39.     [self.window makeKeyAndVisible];  
  40.     return YES;  
  41. }  
  42.   
  43. - (void)applicationWillResignActive:(UIApplication *)application  
  44. {  
  45.     // 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.  
  46.     // 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.  
  47. }  
  48.   
  49. - (void)applicationDidEnterBackground:(UIApplication *)application  
  50. {  
  51.     // 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.   
  52.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.  
  53. }  
  54.   
  55. - (void)applicationWillEnterForeground:(UIApplication *)application  
  56. {  
  57.     // 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.  
  58. }  
  59.   
  60. - (void)applicationDidBecomeActive:(UIApplication *)application  
  61. {  
  62.     // 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.  
  63. }  
  64.   
  65. - (void)applicationWillTerminate:(UIApplication *)application  
  66. {  
  67.     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.  
  68. }  
  69.   
  70. @end  
8、修改FirstViewController.m,初始化时设置tabBarItem的title
显示的图片,仅修改方法initWithNibName,如下:

[cpp] 
  1. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  2. {  
  3.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  4.     if (self) {  
  5.         // Custom initialization  
  6.         self.title = @"First View";  
  7.         self.tabBarItem.image = [UIImage imageNamed:@"first"];  
  8.     }  
  9.     return self;  
  10. }  
9、依照
上步修改SecondViewController.m,如下:

[cpp] 
  1. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  2. {  
  3.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  4.     if (self) {  
  5.         // Custom initialization  
  6.         self.title = @"Second View";  
  7.         self.tabBarItem.image = [UIImage imageNamed:@"second"];  
  8.     }  
  9.     return self;  
  10. }  
10、编译、运行,效果如下

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

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

基于该模板创建的工程就是Tab Bar实现的多视图应用程序,与本文实现的功能相同

转载地址:https://blog.csdn.net/h3c4lenovo/article/details/8307261 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iPhone开发【十】多视图技术总结之三:Navigation
下一篇:iPhone开发【八】多视图技术总结之一:ModalView(模态视图)

发表评论

最新留言

很好
[***.229.124.182]2024年04月10日 06时05分55秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章