首先上效果图:
然后上代码:
创建几个用于切换的控制器,分别命名AviewController,BviewController,CviewController,DviewController
创建一个ViewController,在viewController.m中写以下代码就可以轻松实现控制器的切换了
#import "ViewController.h"
#import "AViewController.h"
#import "BViewController.h"
#import "CViewController.h"
#import "DViewController.h"
@interface ViewController ()
@property(nonatomic,strong)AViewController *aVC;
@property(nonatomic,strong)BViewController *bVC;
@property(nonatomic,strong)CViewController *cVC;
@property(nonatomic,strong)DViewController *dVC;
@end
@implementation ViewController
- (AViewController *)aVC
{
if (!_aVC) {
self.aVC = [[AViewController alloc] init];
self.aVC.view.frame = CGRectMake(0, 110, self.view.frame.size.width, self.view.frame.size.height-110);
}
return _aVC;
}
- (BViewController *)bVC
{
if (!_bVC) {
self.bVC = [[BViewController alloc] init];
self.bVC.view.frame =CGRectMake(0, 110, self.view.frame.size.width, self.view.frame.size.height-110);;
}
return _bVC;
}
- (CViewController *)cVC
{
if (!_cVC) {
self.cVC = [[CViewController alloc] init];
self.cVC.view.frame = CGRectMake(0, 110, self.view.frame.size.width, self.view.frame.size.height-110);
}
return _cVC;
}
- (DViewController *)dVC
{
if (!_dVC) {
self.dVC = [[DViewController alloc] init];
self.dVC.view.frame =CGRectMake(0, 110, self.view.frame.size.width, self.view.frame.size.height-110);
}
return _dVC;
}
- (IBAction)didClickA:(id)sender {
[self.bVC.view removeFromSuperview];
[self.cVC.view removeFromSuperview];
[self.dVC.view removeFromSuperview];
[self.view addSubview:self.aVC.view];
}
- (IBAction)didClickB:(id)sender {
[self.aVC.view removeFromSuperview];
[self.cVC.view removeFromSuperview];
[self.dVC.view removeFromSuperview];
[self.view addSubview:self.bVC.view];
}
- (IBAction)didClickC:(id)sender {
[self.aVC.view removeFromSuperview];
[self.bVC.view removeFromSuperview];
[self.dVC.view removeFromSuperview];
[self.view addSubview:self.cVC.view];
}
- (IBAction)didClickD:(id)sender {
[self.bVC.view removeFromSuperview];
[self.cVC.view removeFromSuperview];
[self.aVC.view removeFromSuperview];
[self.view addSubview:self.dVC.view];
}