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 ...
随机推荐
- 异步编程(AsyncCallback委托,IAsyncResult接口,BeginInvoke方法,EndInvoke方法的使用小总结)
http://www.cnblogs.com/panjun-Donet/archive/2009/03/03/1284700.html 让我们来看看同步异步的区别: 同步方法调用在程序继续执行之前需要 ...
- IT兄弟连 JavaWeb教程 Servlet会话跟踪 Cookie路径问题
操作Cookie时,需要注意路径问题: 设置操作:任何路径都可以设置Cookie,但是有时我们也是用设置进行替换Cookie和删除Cookie(maxAge=0)! 替换:只能由完全相同的路径来操作! ...
- [!] No `Podfile' found in the project directory.
1.cd ios/ 2.vim Podfile(创建Podfile)且输入内容 source'https://github.com/CocoaPods/Specs.git'platform:ios,' ...
- 51Nod 1242 斐波那契数列的第N项(矩阵快速幂)
#include <iostream> #include <algorithm> using namespace std; typedef long long LL; ; ; ...
- redis集群模式
1 弊端和优势 弊端:相比单机模式,集群模式会在节点之间同步数据,会降低20%-30%的性能,同时增加架构复杂性,提高硬件成本和学习成本. 优势:增加冗余,避免单点故障.单机模式如果要重启,必然会丢失 ...
- requests发送HTTPS请求(处理SSL证书验证)
1.SSL是什么,为什么发送HTTPS请求时需要证书验证? 1.1 SSL:安全套接字层.是为了解决HTTP协议是明文,避免传输的数据被窃取,篡改,劫持等. 1.2 TSL:Transport Lay ...
- 移动端meta的使用
伴随着web app的不断火热,移动端可以说是未来的大趋势了,下面是常用的一下meta <!-- 声明文档使用的字符编码 --> <meta charset='utf-8'> ...
- 142. O(1)时间检测2的幂次
用 O(1) 时间检测整数 n 是否是 2 的幂次. 您在真实的面试中是否遇到过这个题? Yes 样例 n=4,返回 true; n=5,返回 false. class Solution { publ ...
- JavaScript实现的9大排序算法
笔试面试经常涉及各种算法,本文简要介绍常用的一些算法,并用JavaScript实现. 1.插入排序 1)算法简介 插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法.它的工作原 ...
- CentOS远程监控
近日,因工作需要,学习了CentOS远程监控的水平有限,多指教. 远程访问CentOS,包括三种方式ssh,telnet,vnc. 本例涉及的是以vnc远程访问CentOS.指令在root下操作.注意 ...