码迷,mamicode.com
首页 > 其他好文 > 详细

Xcode6 下实现界面切换

时间:2015-03-11 14:47:01      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

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)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 <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



Xcode6 下实现界面切换

标签:

原文地址:http://blog.csdn.net/liuyinghui523/article/details/44197935

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!