1.新建视图控制器
2.在AppDelegate中添加
//视图控制器的使用
MainViewController *mainVC = [[MainViewController alloc] init];
// 把一个视图控制器指定为window的根视图控制器
self.window.rootViewController = mainVC;
// 内存管理
[mainVC release];
3.在ViewControl.m中
// 加载视图 系统自己调用
- (void)loadView
{
[super loadView];
// 系统调用这个方法产生一个view, 作为自己自带的视图
NSLog(@"%s", __func__);
}
// 视图已加载
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%s", __func__);
// Do any additional setup after loading the view.
// 控制器自带的视频被创建完毕就会调用这个方法
// 视图创建的代码写在这个地方
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
[button1 setTitle:@"suibian" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor yellowColor];
button1.frame = CGRectMake(20, 20, 280, 50);
[button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
self.view.backgroundColor = [UIColor blackColor];
// 视图控制器的生命周期
}
系统方法:
// 视图将要出现
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"%s", __func__);
}
// 视图已经出现
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"%s", __func__);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
// 当app的内存过高,系统会调用这个方法
// 可以在这个方法中释放掉一些可以重复生成的对象(数据/视图。。)
NSLog(@"内存泄露啦啦啦啦");
}
推出一个新页面(新的视图控制器)
- (void)buttonClicked:(UIButton *)button
{
NSLog(@"点击");
// 模态推出
// 推出一个新的页面(viewController)
// 1, 创建新页面
SecondViewController *secongVC = [[SecondViewController alloc] init];
// 设置推出的动画效果
// UIModalTransitionStyleCoverVertical, //垂直翻页
// UIModalTransitionStyleFlipHorizontal, //水平翻转
// UIModalTransitionStyleCrossDissolve, //溶解消失
// UIModalTransitionStylePartialCurl, //卷曲翻页
[secongVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[secongVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[secongVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[secongVC setModalTransitionStyle:UIModalTransitionStylePartialCurl];
// 2, 推出
// 参数1,要推出的新视图控制器(viewController)
// 参数2,是不是带一个动画效果
[self presentViewController:secongVC animated:YES completion:^{
// 推出新视图之后,要执行的代码
}];
// 3, 内存管理
[secongVC release];
}
返回上一层页面
- (void)buttonClicked:(UIButton *)button
{
[self dismissViewControllerAnimated:YES completion:^{
}];
}