首页
登录 | 注册

iOS开发教程:构建多视图应用程序

大部分的iOS应用都是多视图的应用程序,在iOS 5 SDK的Storyboard中,可以通过NavViewContoller或者TabViewController拖动的方式很轻松的创建多视图应用程序,如下面所示:

为了演示Storyboard中创建ViewSwitcher的方法,我新建了一个项目名叫test,新建时选择SingleView,Xcode会自动创建一个Storyboard给你,打开这个SB,加入一个NavVC,删除原有的View,新建一个View,在navVC的Root View中新建一个RoundRectButton 按住右键连接到新建的View中,在新建的View中加入label以识别,一个简单的nav-based app就完成了,不用编写任何代码。

iOS开发教程:构建多视图应用程序

iOS开发教程:构建多视图应用程序

 

运行来看一看吧:

 

iOS开发教程:构建多视图应用程序

 

iOS开发教程:构建多视图应用程序




但是,使用Storyboard虽然简单,然而却无法兼容iOS 5以下的系统,所以,了解如何使用xib+代码的方式构建多视图app还是很有必要的,下面我们就来讲一讲如何使用这种较为复杂的方式构建多视图应用:

1.创建Multi-view工程

新建EmptyApplication


Class Prefix写成这样是为了方便和以前版本文件命名方式达成一致。

 

以前我们创建多视图多数是使用Window-basedApplication模板,这里我们就开始抛开这种习惯了。window不在用MainWindow.xib来实现,而是在Multi-viewAppDelegate.M的代码里。

这里给出两种模板的代码区别:

Window-basedApplication:

- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

   // Override point for customizationafter application launch.

   [self.windowmakeKeyAndVisible];

   return YES;

}

 

Empty Application:

- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

   self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

   // Override point for customizationafter application launch.

   self.window.backgroundColor = [UIColorwhiteColor];

   [self.windowmakeKeyAndVisible];

   return YES;

}

可以看到这里多了两行实例化window的代码。第一行是实例化一个窗口并指定大小,第二行制定了窗口的颜色。

2.先运行一遍。程序给我们初始化了一个白色的窗口,如果你细心你会发现输出栏有这样的提示:

Applications are expected to have aroot view controller at the end of applicationlaunch



告诉我们需要创建根视图。

我们新建三个视图控制器:RootViewController,FirstViewController和SecondViewController(全部勾选With XIB for user interface)

给三个视图加点东西以便区分开。

 

Root:放上一个工具栏,方便触动切换视图.

First,Second加上一个标签即可。



3.好吧,我们来创建根视图。

Multi-viewAppDelegate.h加入RootViewController

#import <UIKit/UIKit.h>

@class RootViewController;

@interface Multi_viewAppDelegate :UIResponder <UIApplicationDelegate>{

   RootViewController*rootViewController;

}

 

@property (strong,nonatomic) UIWindow *window;

@property (strong,nonatomic) IBOutletRootViewController*rootViewController;

@end

 

修改Multi-viewApplication.m加载一个RootViewController对象为root view controller

#import "Multi_viewAppDelegate.h"

#import "RootViewController.h"

 

@implementation Multi_viewAppDelegate

 

@synthesize window = _window;

@synthesize rootViewController;

 

- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

   self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

   // Override point for customizationafter application launch.

   self.window.backgroundColor = [UIColorwhiteColor];

   self.rootViewController=[[RootViewControlleralloc] initWithNibName:@"RootViewController"bundle:nil];

   self.window.rootViewController=self.rootViewController;

   [self.windowmakeKeyAndVisible];

   return YES;

}

……

运行一遍看一下吧。


4.好啦,现在利用根视图实现在First和Second两个视图之间进行切换。

修改RootViewController.h

#import <UIKit/UIKit.h>

@class FirstViewController;

@class SecondViewController;

 

@interface RootViewController :UIViewController{

   FirstViewController*firstViewControllerl;

   SecondViewController*secondViewController;

}

@property (strong,nonatomic) IBOutletFirstViewController*firstViewController;

@property (strong,nonatomic) IBOutletSecondViewController*secondViewController;

- (IBAction)switchView:(id)sender;

 

@end

 

 

修改RootViewController.m

#import "RootViewController.h"

#import "FirstViewController.h"

#import "SecondViewController.h"

 

@implementation RootViewController

@synthesize firstViewController;

@synthesize secondViewController;

⋯⋯

- (void)viewDidLoad

{

   //程序启动时先加载First视图到Root视图中

   FirstViewController*firstController=[[FirstViewControlleralloc] initWithNibName:@"FirstViewController"bundle:nil];

   self.firstViewController=firstController;

   [self.viewinsertSubview:firstController.viewatIndex:0];

   [super viewDidLoad];

   // Do any additional setup afterloading the view from its nib.

}

⋯⋯

 

- (IBAction)switchView:(id)sender{    
    
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.23];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    if(self.secondViewController.view.superview==nil){//Root视图是否存在,没有就不用做无用功啦        
        if(self.secondViewController==nil){//Second视图控制器不存在,则创建它
            SecondViewController *secondController=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];            
            self.secondViewController=secondController;
        }
        
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
        [firstViewController viewWillAppear:YES];
        [secondViewController viewWillDisappear:YES];
        [firstViewController.view removeFromSuperview];//移走First视图,这里我们就不用担心是否要释放firstViewController啦,ARC会自动帮我们处理的        
        [self.view insertSubview:secondViewController.view atIndex:0];//添加Second视图
        [firstViewController viewDidDisappear:YES];
        [secondViewController viewDidAppear:YES];
    }
    else{
        if(self.firstViewController==nil){            
            FirstViewController *firstController=[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];            
            self.firstViewController=firstController;          
        }
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
        [secondViewController viewWillAppear:YES];
        [firstViewController viewWillDisappear:YES];
        
        [secondViewController.view removeFromSuperview];        
        [self.view insertSubview:firstViewController.view atIndex:0];
        
        [secondViewController viewDidDisappear:YES];
        [firstViewController viewDidAppear:YES];
    }
    [UIView commitAnimations];
}

 

现在只需把操作方法和ToolBar的按钮连起来就行了。

5.好运行,ok。

 





2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.009 s.
京ICP备10005923号