Xcode6 下实现界面切换

Xcode6 下实现界面切换

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

Xcode6 下实现界面切换_第1张图片Xcode6 下实现界面切换_第2张图片


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

Xcode6 下实现界面切换_第3张图片
Xcode6 下实现界面切换_第4张图片

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

Xcode6 下实现界面切换_第5张图片

4.对类进行修改

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

@class SwitchViewController;


@property (nonatomic,retain)IBOutlet SwitchViewController* 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

@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



你可能感兴趣的:(MAC,Xcode,Objective-C)