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 ...
随机推荐
- Codevs 3134 Circle
3134 Circle 题目描述 Description 在一个圆上,有2*K个不同的结点,我们以这些点为端点,连K条线段,使得每个结点都恰好用一次.在满足这些线段将圆分成最少部分的前提下,请计算有多 ...
- IT兄弟连 JavaWeb教程 Servlet会话跟踪 Cookie技术原理
Cookie使用HTTPHeader传递数据.Cookie机制定义了两种报头,Set-Cookie报头和Cookie报头.Set-Cookie报头包含于Web服务器的响应头(ResponseHeade ...
- Django框架的安装,项目创建
目录 Django框架的安装,项目创建 方法安装 Django版本选择 1.11.21(最新LTS版) django管理目录 命令行创建项目 django项目 命令行启动 (必须在项目文件下执行) p ...
- 面向对象-mixin设计模式的应用(多继承应用场景)
什么是设计模式? 设计模式只是一种开发思想.不是什么固定的格式. 前人的好的思想,我们后人拿过来用! mixin设计模式: 1.mixin设计迷失可以在不对类的内容的修改前提下,扩展类的功能(添加父类 ...
- java 阻塞队列(转)
转自 http://ifeve.com/java-blocking-queue/ 1. 什么是阻塞队列? 阻塞队列(BlockingQueue)是一个支持两个附加操作的队列.这两个附加的操作是:在队列 ...
- mysql8必知必会7 连接 内连接 外连接 交叉连接
/* 2 需求:建立产品和分类表 1.查询每种分类的产品数量,没有产品的分类也要统计.(cname,quantity) 2.根据分类名称查询分类中的所有产品 */ -- --------------- ...
- 字典转json
1.字典转json -(NSString*)dictionaryToJson:(NSDictionary *)dic { NSError *parseError = nil; NSData *json ...
- 关于controller返回的页面js文件和css文件404问题的正式解决
谨用此博客记录一下这条卡了两个星期的bug…… 还是之前的问题,通过get方法,后台@Controller返回页面然后弹窗.但是不知道为什么一直所有js文件和css文件都报404…… (之前的博客记录 ...
- freertos之任务
taskYIELD(): 通知调度器自己放弃运行态,可立即进行任务切换,而不必等到当前任务的时间片耗尽.这对于相同任务优先级的2个任务来说可加速效率.
- 使用SpringCloud-Netflix
目录 SpringCloud-Netflix 配置统一依赖管理 创建服务注册中心 创建服务提供者 创建服务消费者 SpringCloud-Netflix Spring Cloud 是一个相对比较新的微 ...