一.视图控制器的声明周期
- //视图从storyboard或者xib文件中加载
- -(void)awakeFromNib
- {
- NSLog(@"awake from nib");
- }
- //视图将要显示
- -(void)viewWillAppear:(BOOL)animated
- {
- NSLog(@"viewWillAppear");
- }
- //视图创建完成
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSLog(@"viewDidLoad");
- }
- //视图将要隐藏
- -(void)viewWillDisappear:(BOOL)animated
- {
- NSLog(@"viewWillDisappear");
- }
- //视图已经隐藏
- -(void)viewDidDisappear:(BOOL)animated
- {
- NSLog(@"viewDisappear");
- }
- //视图已经显示
- -(void)viewDidAppear:(BOOL)animated
- {
- NSLog(@"viewDidappear");
- }
- //视图将要布局子视图
- -(void)viewWillLayoutSubviews
- {
- NSLog(@"will layout subviews");
- }
- //视图已经完成布局子视图
- -(void)viewDidLayoutSubviews
- {
- NSLog(@"view did layout subviews");
- }
- //视图已经销毁
- -(void)viewDidUnload
- {
- NSLog(@"viewDidUnload");
- }
二.通过empty模板的方式实现从一个控制器中打开另外一个控制器
1.建立普通模板,将Main.storyboard删除掉,然后设置main interface 为空
2.AppDelegate.m代码段如下
- #import "AppDelegate.h"
- #import "ViewController.h"
- @interface AppDelegate ()
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- //创建window
- self.window = [[UIWindow alloc]init];
- CGSize screenSize = [[UIScreen mainScreen]bounds].size;
- //设置window的frame
- self.window.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);
- //设置根控制器
- ViewController *viewController = [[ViewController alloc]init];
- [self.window setRootViewController:viewController];
- //接收用户响应并显示
- [self.window makeKeyAndVisible];
- return YES;
- }
3.viewController.m代码段如下:
- #import "ViewController.h"
- #import "AppDelegate.h"
- #import "SecondViewController.h"
- @interface ViewController ()
- @property(strong,nonatomic)UIButton *button;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor redColor];
- self.button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
- [self.button setTitle:@"second" forState:UIControlStateNormal];
- [self.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:self.button];
- }
- //按钮的单击事件
- -(void)buttonClicked:(UIButton *)sender
- {
- NSLog(@"clicked");
- //方式1.通过AppDdelegate获取UIWindow
- // AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
- // UIWindow *window = delegate.window;
- //方式2.通过UIApplication直接获取UIWindow
- UIWindow *window = [UIApplication sharedApplication].keyWindow;
- //使用window切换控制器
- SecondViewController *second = [[SecondViewController alloc]init];
- //将一个控制器设置成rootViewController控制器,就可以打开这个新的控制器
- [window setRootViewController:second];
- }