首先我们要新建2个类,因为我们要实现从第一个页面跳转到第二个页面。第一个叫FirstViewController 继承自UIViewController.第二个叫SecondViewController,也是继承自UIViewController.然后给他们加上背景颜色,在BOOL返回值里面,第一个是蓝色,第二个是棕色。
我们先在AppDelegate里面压入根视图控制器,具体代码如下。
#import "AppDelegate.h"
#import "FirstViewController.h"//导入第一个视图
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//创建视图控制器
FirstViewController *firstVC = [[FirstViewController alloc]init];
//设置视图控制器设为窗口的根视图控制器
self.window.rootViewController = firstVC;
[self.window makeKeyAndVisible];
return YES;
}
下面我来设置FirstViewController
#import "FirstViewController.h"
#import "SecondViewController.h"// 导入要进入的页面
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
//创建btn按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(20, 30, 250, 40);
[btn setTitle:@"进入下一个页面" forState:UIControlStateNormal];
//调用下面的btnClick
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
btn.titleLabel.font = [UIFont systemFontOfSize:22];
[self.view addSubview:btn];
}
-(void)btnClick
{
//设置要进入的页面
SecondViewController *secondVC = [[SecondViewController alloc]init];
//设置转变模式,为反转分格
secondVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//现在开启动画
[self presentViewController:secondVC animated:YES completion:nil];
}
下载已经可以跳入第二个页面了,那么我们要怎么返回呢?下面我来做点事情进入SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置背景颜色
self.view.backgroundColor = [UIColor brownColor];
//创建一个btn按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(20, 30, 250, 40);
[btn setTitle:@"返回页面" forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:22];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)btnClick
{
//返回到之前的视图控制器
[self dismissViewControllerAnimated:YES completion:nil];
}