Xcode6 下实现界面切换

来源:互联网 发布:大数据资格认证doop 编辑:程序博客网 时间:2024/06/15 18:37

Xcode6 下实现界面切换

1.新建一个工程T  Xcode ——> File——>New--->Project--->Single View Application



 2. 添加一个三个Cocoa Touch Class 基类为UIViewController类,SwitchViewController、FirstViewController、SecondViewController。


3.在storyBoard中创建两个View Controller,分别设置Storyboard ID为first 和 second,Class为FIrstViewController 和 SecondViewController。并在界面上各放一个label和button


4.对类进行修改

  1.在AppDelegate.h文件中,添加

@class SwitchViewController;


@property (nonatomic,retain)IBOutletSwitchViewController* switchViewCOntroller;

+(AppDelegate*) app;

2.在AppDelegate.m文件中添加,

#import "SwitchViewController.h"

修改didFinishLaunchingWithOptions函数

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

self.switchViewCOntroller=[[SwitchViewControlleralloc]init];

[self.switchViewCOntrollerinitView];

self.window.rootViewController=self.switchViewCOntroller;

self.window.backgroundColor=[UIColorwhiteColor];

[self.windowmakeKeyAndVisible];

returnYES;

}

添加

+(AppDelegate*) app{

 

  return (AppDelegate *) [[UIApplicationsharedApplication]delegate];


}

3.修改SwitchViewCOntroller类

 1.头文件修改成

#import <UIKit/UIKit.h>

@class FirstViewController;

@class SecondViewController;

@interface SwitchViewController :UIViewController{

FirstViewController*  firstViewController;

SecondViewController* secondViewController;

}

@property(nonatomic ,retain)FirstViewController*  firstViewController;

@property(nonatomic ,retain)SecondViewController* secondViewController;

-(void) initView;

-(void) showFirstView;

-(void) showSecondView;

-(void) removeAllView;


@end

 2.m文件中完成添加的方法。

#import "FirstViewController.h"

#import "SecondViewController.h"


-(void) initView{

if (self.firstViewController ==nil) {

UIStoryboard *futureStoryBoard = [UIStoryboardstoryboardWithName:@"Main"bundle:nil];

self.firstViewController = [futureStoryBoardinstantiateViewControllerWithIdentifier:@"first"];


}

[selfremoveAllView];

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

}

-(void) showFirstView{

if (self.firstViewController ==nil) {

UIStoryboard *futureStoryBoard = [UIStoryboardstoryboardWithName:@"Main"bundle:nil];

self.firstViewController = [futureStoryBoardinstantiateViewControllerWithIdentifier:@"first"];


}

[selfremoveAllView];

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

}

-(void) showSecondView{

if (self.secondViewController ==nil) {

UIStoryboard *futureStoryBoard = [UIStoryboardstoryboardWithName:@"Main"bundle:nil];

self.secondViewController = [futureStoryBoardinstantiateViewControllerWithIdentifier:@"second"];

}

[selfremoveAllView];

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

}


-(void) removeAllView{

int t=[self.view.subviewscount];

NSLog(@"%d",t);

for (int i=0;i<[self.view.subviewscount];i++) {

[[self.view.subviewsobjectAtIndex:i]removeFromSuperview];

}

}

5.为两个界面上的按钮分别添加消息响应函数

firstViewController添加

- (IBAction)buttonClick:(id)sender {

[[AppDelegateapp].switchViewCOntroller showSecondView];

}

secondViewController添加

- (IBAction)buttonClick:(id)sender {

[[AppDelegateapp].switchViewCOntroller showFirstView];

}


为了大家写代码时能有完整参考,特传上了源码

http://download.csdn.net/detail/liuyinghui523/8491405



0 0