ViewController

ViewController是IOS开发中MVC模式中的C,ViewController是view的controller,ViewController的职责主要包括管理内部各个view的加载显示和卸载,同时负责与其他ViewController的通信和协调。

在IOS中,有两类ViewController,一类是显示内容的,比如UIViewController、UITableViewController等,同时还可以自定义继承自UIViewController的ViewController;另一类是ViewController容器,UINavigationViewController和UITabBarController等,UINavigationController是以Stack的形式来存储和管理ViewController,UITabBarController是以Array的形式来管理ViewController。

 

View的加载

【Xamarin 开发 IOS --IOS ViewController生命周期】

可以看到,在Controller中创建View有2种方法,一种是使用Storyboard等可视化工具,另一种是通过代码创建。可视化创建在工程中很少用到,因为它满足不了开发者如饥似渴的需求--。

 

代码创建界面文件

1.创建新的Empty Application Project

2.新建ViewController的类,添加loadView方法,及viewDidLoad等方法

【Xamarin 开发 IOS --IOS ViewController生命周期】
//
//  XYZViewController.m
//  ViewLifeCycle
//
//  Created by Norcy on 14-7-28.
//  Copyright (c) 2014年 QQLive. All rights reserved.
//

#import "XYZViewController.h"

@interface XYZViewController ()

@end

@implementation XYZViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView
{
    UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    
    contentView.backgroundColor = [UIColor blueColor];
    
    self.view = contentView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"View Did Load");
}

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"View Will Appear");
}

- (void)viewDidAppear:(BOOL)animated
{
    NSLog(@"View Did Appear");
}

- (void)viewWillDisappear:(BOOL)animated
{
    NSLog(@"View Will Disappear");
}

- (void)viewDidDisappear:(BOOL)animated
{
    NSLog(@"View Did Disappear");
}
@end
【Xamarin 开发 IOS --IOS ViewController生命周期】

3.在AppDelegate.m中的application:didFinishLaunchingWithOptions:中注册ViewController

【Xamarin 开发 IOS --IOS ViewController生命周期】
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    XYZViewController *viewController = [[XYZViewController alloc]initWithNibName:nil bundle:nil];
    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
【Xamarin 开发 IOS --IOS ViewController生命周期】

相关文章:

  • 2021-11-07
  • 2022-12-23
  • 2021-09-12
  • 2021-05-24
  • 2021-10-04
  • 2021-07-22
  • 2022-01-24
  • 2021-05-20
猜你喜欢
  • 2021-08-29
  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
  • 2021-09-30
相关资源
相似解决方案