一、生命周期
1、ViewDidLoad: 一般的初始化,除了几何图形的初始化(这个时候还没确定)
2、ViewWillAppear: 代表你的视图将要在屏幕上显示,可能会调用多次,对不可见时可能能改变的内容进行同步 (例如模型改变的时候调用显示改变的数据)
3、ViewWillDisappear 不在屏幕不再占用资源时,记住、恢复、停止
4、didRecevierMemoryWarning 手机运行占用很大空间,例如图像、视频:释放内存,处理内存警告(系统决定)
具体的生命周期是这样的:
(1 从storyboard中进行实例化,或者通过调用all从或者init,
(2 如果是从storyboard中出来的,就会调用awakeFromNib,不然就是调用initWithNibName bundle
(3 viewDidLoad
(4 当几何内容被确定之后,viewWillLayoutSubview和viewDidlayoutSubviews被调用
(5.1 viewWillappear和viewDidappear会被调用
(5.2 viewWillDisappear和viewDidDisappear会被调用 (如果几何内容有变化,viewWillLayoutSubview和viewDidlayoutSubviews会再次被调用)
(6 didRecevierMemoryWarning
二、NSNotifications 广播站机制(通知)
结束收听之后,要移除自己【不安全指针】,可以在dealloc移除
例子:
//
// ViewController.m
// testForNotification
//
// Created by bos on 15-4-14.
// Copyright (c) 2015年 axiba. All rights reserved.
// #import "ViewController.h" @interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *content;
@property (weak, nonatomic) IBOutlet UILabel *headLine; @end @implementation ViewController - (IBAction)changeTextColorByClickButton:(UIButton*)sender { //control the range of our select
[self.content.textStorage addAttribute:NSForegroundColorAttributeName
value:sender.backgroundColor
range:self.content.selectedRange]; } - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. } #pragma the view appear
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setUserPreferFont)
name:UIContentSizeCategoryDidChangeNotification
object:nil]; } #pragma the view disappear then remove it
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setUserPreferFont)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
-(void)setUserPreferFont
{
self.headLine.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
self.content.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end