`
shuai1234
  • 浏览: 932462 次
  • 性别: Icon_minigender_1
  • 来自: 山西
社区版块
存档分类
最新评论

Iphone开发(八)利用Tabbed Application模板实现多视图切换

 
阅读更多

holydancer原创,如需转载,请在显要位置注明:

转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7417030

 

在android中我们一般用intent意图来在各个view之间跳转,在以前的ios开发中,如果要实现选项卡切换多视图,需要用到一个现在Xcode版本中没有的模板Window-Based Application模板,然后在里面建一堆的viewController和xib文件,然后再一顿的猛连,新手到这里往往会很痛苦。现在好了,新版的Xcode中的Tabbed Application模板可以简单明了的创建一个选项卡多视图,甚至我们都不需要任何操作。

下面我们先打开Xcode,然后选择该模板创建项目:

创建成功后直接运行就会在虚拟机上出现一个选项卡(两个选项),点击就会切换不同的view;

 

现在我们来分析一下这个模板,了解后我们就可以利用该模板来创建我们自己的应用了。

首先该模板并没有主xib文件,所以我们要去AppDelegate中查看是如何代码生成的,在该类的声明中我们发现如下代码:

AppDelegate.h:

 

[plain] view plaincopy
 
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>  
  4.   
  5. @property (strong, nonatomic) UIWindow *window;  
  6.   
  7. @property (strong, nonatomic) UITabBarController *tabBarController;  
  8.   
  9. @end  


UIWindow下面就是一个UITabBarController,这里并不是我们常见的viewController,而是一个UIViewController的子类。该类的声明如下:

 

 

[plain] view plaincopy
 
  1. UIKIT_CLASS_AVAILABLE(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding> {  
  2.   @package  
  3.     UITabBar               *_tabBar;  
  4.       
  5.     UIView                 *_containerView;  
  6.     UIView                 *_viewControllerTransitionView;  
  7.   
  8.     id                      _tabBarItemsToViewControllers;  
  9.     UIViewController       *_selectedViewController;  
  10.   
  11.     UINavigationController *_moreNavigationController;  
  12.     NSArray                *_customizableViewControllers;  
  13.       
  14.     id<UITabBarControllerDelegate> _delegate;  
  15.       
  16.     UIViewController       *_selectedViewControllerDuringWillAppear;  
  17.           
  18.     UIViewController       *_transientViewController;  
  19.       
  20.     NSUInteger              _maxItems;  
  21.       
  22.     struct {  
  23.     unsigned int isShowingMoreItem:1;  
  24.     unsigned int needsToRebuildItems:1;  
  25.     unsigned int isBarHidden:1;  
  26.     unsigned int editButtonOnLeft:1;  
  27.     } _tabBarControllerFlags;  
  28. }  


从上面我们可以发现,该控制器是自带一个UITabBar的,而且里面是有UIViewController作为属性的。现在我们来看AppDelegate的实现:

 

AppDelegate.m:

 

[plain] view plaincopy
 
  1. #import "AppDelegate.h"  
  2.   
  3. #import "FirstViewController.h"  
  4.   
  5. #import "SecondViewController.h"  
  6. #import "ThirdViewController.h"  
  7.   
  8. @implementation AppDelegate  
  9.   
  10. @synthesize window = _window;  
  11. @synthesize tabBarController = _tabBarController;  
  12.   
  13. - (void)dealloc  
  14. {  
  15.     [_window release];  
  16.     [_tabBarController release];  
  17.     [super dealloc];  
  18. }  
  19.   
  20. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  21. {  
  22.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];//1  
  23.     // Override point for customization after application launch.  
  24.     UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];//2  
  25.     UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];//3  
  26.       
  27.    // UIViewController *viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil] autorelease];//4  
  28.       
  29.     self.tabBarController = [[[UITabBarController alloc] init] autorelease];//5  
  30.     self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3,nil];//6  
  31.     self.window.rootViewController = self.tabBarController;//7  
  32.     [self.window makeKeyAndVisible];//8  
  33.     return YES;  
  34. }  
  35.   
  36.   
  37. @end  


没用到的方法被我删掉,现在主要来看应用初始化方法

 

代码1初始化窗口;

代码2初始化view1的控制器;

代码3初始化view2的控制器;

代码4是我自己后来加的,这里先不管;

代码5初始化根视图控制器(这时只是一般的视图控制器);

代码6将那两个视图的控制器添加为数组给了根视图控制器的viewControllers属性,以便显示;

代码7将根视图器转正(真正成为根视图控制器);

代码8显示窗口;

另外,选项卡上的选项文字和图标是在每一个view的viewController初始化方法里确定的,比如:

FirstViewController.m:

 

[plain] view plaincopy
 
  1. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  2. {  
  3.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  4.     if (self) {  
  5.         self.title = NSLocalizedString(@"First", @"First");  
  6.         self.tabBarItem.image = [UIImage imageNamed:@"first"];  
  7.     }  
  8.     return self;  
  9. }  

 

[plain] view plaincopy
 
  1. self.title = NSLocalizedString(@"First", @"First");  

上面这行代码是给标题,NSLocalizedString方法只是一个宏定义,第二个参数无用,你也可以直接self.title = @"First”这样来赋值。

 

好了,上面这些模板是系统给我们的,包括图标素材,我们也大概了解了是怎样添加view到应用里和选项卡上的选项对应,我们现在来手动再添加一个view

首先,新建一个viewController,取名为ThirdViewController,注意附带xib文件。

然后在xib文件中拖上一个segmented控件(随便什么都行),将Bottom Bar那一栏修改为Tab Bar用以为选项卡留出位置(好习惯),如下图

在ThirdViewController.m中初始化方法中设置选项名和图标:

 

[plain] view plaincopy
 
  1. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  2. {  
  3.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  4.     if (self) {  
  5.           
  6.         self.title=@"自定义的";  
  7.   
  8.         self.tabBarItem.image = [UIImage imageNamed:@"second"];  
  9.         //这里我们就用和第二个一样的图片  
  10.   
  11.     }  
  12.     return self;  
  13. }  



 

现在将AppDelegate文件在原来的基础上做如下修改:

1,导入新建类ThirdViewController的头文件;

2,将该类实例化,对象名按惯例取名为viewController3;

3,将实例化后的viewController3加到数组里。

 

[plain] view plaincopy
 
  1. #import "AppDelegate.h"  
  2.   
  3. #import "FirstViewController.h"  
  4.   
  5. #import "SecondViewController.h"  
  6. #import "ThirdViewController.h"//新增  
  7.   
  8. @implementation AppDelegate  
  9.   
  10. @synthesize window = _window;  
  11. @synthesize tabBarController = _tabBarController;  
  12.   
  13. - (void)dealloc  
  14. {  
  15.     [_window release];  
  16.     [_tabBarController release];  
  17.     [super dealloc];  
  18. }  
  19.   
  20. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  21. {  
  22.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  23.     // Override point for customization after application launch.  
  24.     UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];  
  25.     UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];  
  26.       
  27.     UIViewController *viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil] autorelease];//新增  
  28.       
  29.     self.tabBarController = [[[UITabBarController alloc] init] autorelease];  
  30.     self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3,nil];//添加  
  31.     self.window.rootViewController = self.tabBarController;  
  32.     [self.window makeKeyAndVisible];  
  33.     return YES;  
  34. }  
  35.   
  36.   
  37. @end  


好了,见证奇迹吧:

 

这时我们就实现了在系统模板的基础上添加view,也可以修改原有的,这样就可以很简单的在多个视图之间进行切换,so easy。

 

关键字:Iphone ,IOS ,Iphone开发 ,基础 ,选项卡栏  ,UITabBar ,多视图切换 ,自定义多视图切换 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics