多视图控制器基础篇

一.视图控制器的声明周期

  1. //视图从storyboard或者xib文件中加载
  2. -(void)awakeFromNib
  3. {
  4.     NSLog(@"awake from nib");
  5. }
  6. //视图将要显示
  7. -(void)viewWillAppear:(BOOL)animated
  8. {
  9.     NSLog(@"viewWillAppear");
  10. }
  11. //视图创建完成
  12. - (void)viewDidLoad
  13. {
  14.     [super viewDidLoad];
  15.     NSLog(@"viewDidLoad");
  16. }
  17. //视图将要隐藏
  18. -(void)viewWillDisappear:(BOOL)animated
  19. {
  20.     NSLog(@"viewWillDisappear");
  21. }
  22. //视图已经隐藏
  23. -(void)viewDidDisappear:(BOOL)animated
  24. {
  25.     NSLog(@"viewDisappear");
  26. }
  27. //视图已经显示
  28. -(void)viewDidAppear:(BOOL)animated
  29. {
  30.     NSLog(@"viewDidappear");
  31. }
  32. //视图将要布局子视图
  33. -(void)viewWillLayoutSubviews
  34. {
  35.     NSLog(@"will layout subviews");
  36. }
  37. //视图已经完成布局子视图
  38. -(void)viewDidLayoutSubviews
  39. {
  40.     NSLog(@"view did layout subviews");
  41. }
  42. //视图已经销毁
  43. -(void)viewDidUnload
  44. {
  45.     NSLog(@"viewDidUnload");
  46. }

 

二.通过empty模板的方式实现从一个控制器中打开另外一个控制器

1.建立普通模板,将Main.storyboard删除掉,然后设置main interface 为空

2.AppDelegate.m代码段如下

  1. #import "AppDelegate.h"
  2. #import "ViewController.h"
  3. @interface AppDelegate ()
  4. @end
  5. @implementation AppDelegate
  6. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  7.     // Override point for customization after application launch.
  8.     //创建window
  9.     self.window = [[UIWindow alloc]init];
  10.     CGSize screenSize = [[UIScreen mainScreen]bounds].size;
  11.     //设置window的frame
  12.     self.window.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);
  13.     //设置根控制器
  14.     ViewController *viewController = [[ViewController alloc]init];
  15.     [self.window setRootViewController:viewController];
  16.     //接收用户响应并显示
  17.     [self.window makeKeyAndVisible];
  18.     return YES;
  19. }

 3.viewController.m代码段如下:

  1. #import "ViewController.h"
  2. #import "AppDelegate.h"
  3. #import "SecondViewController.h"
  4. @interface ViewController ()
  5. @property(strong,nonatomic)UIButton *button;
  6. @end
  7. @implementation ViewController
  8. - (void)viewDidLoad
  9. {
  10.     [super viewDidLoad];
  11.     self.view.backgroundColor = [UIColor redColor];
  12.     self.button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
  13.     [self.button setTitle:@"second" forState:UIControlStateNormal];
  14.     [self.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
  15.     [self.view addSubview:self.button];
  16. }
  17. //按钮的单击事件
  18. -(void)buttonClicked:(UIButton *)sender
  19. {
  20.     NSLog(@"clicked");
  21.     //方式1.通过AppDdelegate获取UIWindow
  22. //    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
  23. //    UIWindow *window = delegate.window;
  24.     //方式2.通过UIApplication直接获取UIWindow
  25.     UIWindow *window = [UIApplication sharedApplication].keyWindow;
  26.     //使用window切换控制器
  27.     SecondViewController *second = [[SecondViewController alloc]init];
  28.     //将一个控制器设置成rootViewController控制器,就可以打开这个新的控制器
  29.     [window setRootViewController:second]; 
  30. }

 

全部评论

相关推荐

头像
11-06 10:58
已编辑
门头沟学院 嵌入式工程师
双非25想找富婆不想打工:哦,这该死的伦敦腔,我敢打赌,你简直是个天才,如果我有offer的话,我一定用offer狠狠的打在你的脸上
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务