@class SwitchViewController;
@property (nonatomic,retain)IBOutlet SwitchViewController* switchViewCOntroller;
+(AppDelegate*) app;
#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];
}
#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
#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];
}
}
firstViewController添加
- (IBAction)buttonClick:(id)sender {
[[AppDelegateapp].switchViewCOntroller showSecondView];
}
secondViewController添加- (IBAction)buttonClick:(id)sender {
[[AppDelegateapp].switchViewCOntroller showFirstView];
}
为了大家写代码时能有完整参考,特传上了源码
http://download.csdn.net/detail/liuyinghui523/8491405