一,UIViewController的使用
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
[_windowrelease];
// UIViewController的使用
// UIViewController是一个抽象类,抽象类不可以直接使用,只能通过创建它的子类去使用。
MainViewController *mainVC = [[MainViewControlleralloc] init];
// 将mainVC指定为window的根视图控制器,作为首页显示
self.window.rootViewController = mainVC;
// 内存管理
[mainVC release];
returnYES;
}
- (void)dealloc
{
[_windowrelease];
[superdealloc];
}
二,创建UIViewController
#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(@"初始化视图控制器");
}
returnself;
}
- (void)loadView
{
[superloadView];
NSLog(@"加载视图");
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"加载视图完毕");
// 一个视图控制器的生命周期
// 1. 初始化方法
// 2. 在view显示之前,先调用loadView方法加载view
// 3. 调用viewDidLoad方法,对视图结构进行布局
// 4. viewWillAppear: 方法
// 5. viewDidAppear: 方法
// 每个视图控制器都自己带一个view,方便开发者添加视图
// self.view.backgroundColor = [UIColor purpleColor];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame =CGRectMake(20,200, 80,50);
button.backgroundColor = [UIColororangeColor];
[button setTitle:@"点击"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
- (void)buttonClicked:(UIButton *)button
{
NSLog(@"GOOD");
// 弹出第二个页面
// 1. 创建第二个页对象
SecondViewController *secondVC = [[SecondViewControlleralloc] init];
[secondVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
// 2. 弹出
[selfpresentViewController:secondVC animated:YEScompletion:^{
NSLog(@"完成弹出");
}];
// 3. 内存管理
[secondVC release];
}
- (void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
NSLog(@"视图将要出现");
}
- (void)viewDidAppear:(BOOL)animated
{
[superviewDidAppear:animated];
NSLog(@"视图已经出现");
}
- (void)viewWillDisappear:(BOOL)animated
{
[superviewWillDisappear:animated];
NSLog(@"视图将要消失");
}
- (void)viewDidDisappear:(BOOL)animated
{
[superviewDidDisappear:animated];
NSLog(@"视图已经消失");
}
三,第二个UIViewController页面
#import "SecondViewController.h"
#import "MainViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(50, 300, 100, 80);
button.backgroundColor = [UIColor whiteColor];
[button setTitle:@"点击" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClicked:(UIButton *)button
{
NSLog(@"great");
// 干掉自己
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"返回结束");
}];
}