Xcode6 下实现界面切换

本文详细介绍如何在Xcode6中使用Storyboard实现界面间的切换。通过创建不同ViewController并利用SwitchViewController进行界面展示与切换,文章提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/detail/liuyinghui523/8491405



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值