CS193p Lecture 5 - View Controller Lifecycle
1. UITextView
@property(nonatomic,readonly,retain) NSTextStorage *textStorage
是 NSMutableAttributedString 的子类
[self.body.textStorage addAttributes:@{ NSStrokeWidthAttributeName : @-3,
NSStrokeColorAttributeName : [UIColor blackColor]}
range:self.body.selectedRange];
这里的 NSStrokeWidthAttributeName 的 -3 和 3 的区别是:
0表示无描边,正数表示描边,负数表示描边+填充;
假设原文字是(foreground color)蓝色的,用加了正数属性后,就仅有描边,成为空心轮廓的样式
2. View Controller Lifecycle(视图控制器生命周期)
所谓的视图控制器生命周期,其实就是一系列的方法,当事件发生时,会被发送至 UIViewController。如果需要重写这些方法,记得先super,例如:[super viewDidLoad];
解释下super:
向super发送消息时,系统在查找方法时会跳过当前对象的类,从父类开始查询,相当于是先调用了父类的同名方法。
viewDidLoad
- (void)viewDidLoad
适合放置视图控制器的初始化代码,一个生命周期中只会调用一次;
输出口(Outlet)已经设置好,故可以设置各种UI的初始化展示;
有一类操作不适合添加:关于视图几何的代码,因为在 viewDidLoad 被调用时,视图的边界(bounds)还没有定下来,它可能不会出现在预定位置,屏幕可能旋转之类的;总之,关于视图的大小、位置(统称几何)的代码不适合放在 viewDidLoad 中;
viewWillAppear
- (void)viewWillAppear:(BOOL)animated
每次视图重新在屏幕上显示时,viewWillAppear 就会被调用;
此时视图的几何信息已经设置,在这里可以执行一些基于几何的初始化;
viewWillDisappear
- (void)viewWillDisappear:(BOOL)animated
当视图从屏幕离开时,viewWillDisappear会被调用;
可以做些记住当前状态或保存数据的操作,以便重新回到视图时恢复;
viewDidAppear
- (void)viewDidAppear:(BOOL)animated
viewDidDisappear
- (void)viewDidDisappear:(BOOL)animated
viewWillLayoutSubviews
- (void)viewWillLayoutSubviews
当视图的frame变化,子视图重现布局时被调用,比如:屏幕旋转;
viewDidLayoutSubviews
- (void)viewDidLayoutSubviews
Between "will" and "did", autolayout will happen;
didReceiveMemoryWarning
- (void)didReceiveMemoryWarning
系统内存不足时会发送这个消息;
收到这个警告时,需要释放内存,也就是释放堆中的内容,也就是将强指针设为nil;
- (void)setup{ };
- (void)awakeFromNib{
[self setup];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
[self setup];
return self;
}
3. NSNotification
The “radio station” from the MVC slides.
radio station 包括了收听(tune into)消息和广播(broadcast)消息两块,本节只介绍收听的;
收听里面又分了收听系统消息和收听自定义的来自模型的消息,本节只介绍收听系统消息;
每一个iOS应用中都有一个 NSNotificationCenter 对象,对象可以将自己注册为某个通知的观察者(observer),例如:“如果有人找到了我丢失的
CS193p Lecture 5 - View Controller Lifecycle的更多相关文章
- iOS 因为reason: 'Pushing the same view controller instance more than once is not supported而奔溃(下)
这个问题是什么意思呢,之前遇到过几次,但程序再次打开时没有问题,也就没有重视,今天又遇到了,无法忍受啊. 控制台报的错误是:"不支持多次推入相同的视图控制器实例". 什么原因造成的 ...
- 报错:Failed to instantiate the default view controller for UIMainStoryboardFile 'MainStoryboard' - perhaps the designated entry point is not set?
原因分析:在StoryBoard中没有一个view controller设置了Initial Scene. 解决方案:在Storyboard中,选择一个view conroller作为story bo ...
- iOS架构师之路:控制器(View Controller)瘦身设计
前言 古老的MVC架构是容易被iOS开发者理解和接受的设计模式,但是由于iOS开发的项目功能越来越负责庞大,项目代码也随之不断壮大,MVC的模糊定义导致我们的业务开发工程师很容易把大量的代码写到视图控 ...
- View Controller Relationships
Parent-child relationshipsParent-child relationships are formed when using view controller container ...
- Model View Controller
On the iPhone or iPod touch, a modal view controller takes over the entire screen. This is the defau ...
- UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
Attempting to load the view of a view controller while it is deallocating is not allowed and may res ...
- Application tried to present a nil modal view controller on target “Current View Controller”解决方案
情景再现 1,自定义一个storyboard: 打开xcode,按下cmd+N,新建一个Storyboard--->next 将新建立的storyboard命名为:TestViewControl ...
- 【IOS笔记】View Controller Basics
View Controller Basics 视图控制器基础 Apps running on iOS–based devices have a limited amount of screen s ...
- UIStoryboard类介绍(如何从Storyboard中加载View Controller)
如何从Storyboard中加载View Controller? 1. 首先了解下UIStoryboard类: @class UIViewController; @interface UIStoryb ...
随机推荐
- 如何有效提升Unity Gear VR游戏性能
http://www.vr186.com/vr_news/vr_technical_area/1093.html 好的,所以你决定用 Unity 来做一个 VR 游戏,并选定了三星 Gear vr 为 ...
- PUSH 和 远程推送
1. UIApplacation向 APNS 注册 push notification 服务 (1) 应用程序要支持推送服务, 在网页里面配置 http://developer.apple.com/ ...
- 新手安装 hadoop、hive和hbase 笔记
系统是ubuntu 12.04 , hadoop版本是1.2.1 , hive版本是0.12 , hbase版本我忘记了,不好意思首先是配置好hostnamevi /etc/hosts写入你要配置的i ...
- 线程安全 原子性 可见性 顺序性 volatile
- 网络请求方法(SDK封装可以替换afn)
//个人觉得 sdk开发时候 最好不要用第三方 最好可以用最原始的方法 替换 此处仅做sdk封装使用 留存+(void)GET:(NSString *)urlStr params:(NSDiction ...
- Codeforces Round #547 (Div. 3) B.Maximal Continuous Rest
链接:https://codeforces.com/contest/1141/problem/B 题意: 给n个数,0代表工作,1代表休息,求能连续最大的休息长度. 可以连接首尾. 思路: 求普通连续 ...
- BZOJ1218(线段树+扫描线)
1.扫描线扫描x轴,线段树维护y轴. 2.坐标+1,线段树是从1开始维护.然后让边长--,这样就能包含边上的点了. 3.为了保证点在正方形内:在x轴上利用差分的思想,在x出Add(val),在x+r( ...
- nodejs 学习(4) express+mongoose
一.关于安装和启动: 1.设置环境变量:D:\Program Files\MongoDB\bin 2.启动时需要cd到bin 目录,然后 mongod --dbpath "D:\mongdb ...
- git reflog查看所有操作记录
git reflog 可以查看所有分支的所有操作记录(包括(包括commit和reset的操作),包括已经被删除的commit记录,git log则不能察看已经删除了的commit记录 具体一个例子, ...
- 渣渣菜鸡为什么要看 ElasticSearch 源码?
前提 人工智能.大数据快速发展的今天,对于 TB 甚至 PB 级大数据的快速检索已然成为刚需,大型企业早已淹没在系统生成的浩瀚数据流当中.大数据技术业已集中在如何存储和处理这些海量的数据上.Elast ...