iOS生命周期 & 通知中心
笔记内容
学习笔记-段玉磊 Stanford course
View Controller Lifecycle
这篇文是我记载
Developing iOS 7 Apps公开课第5课的笔记
UITextView
Set its text and attributes via its NSMutableAttributedString
使用UITextView 要属性NSTextStorage类型
@property (nonatomic, readonly) NSTextStorage *textStorage;
NSTextStorage 是NSMutableAttributedString的一个子类
利用NSTextStorage更简洁的修改UITextView的更新,例如字体颜色。
View Controller Lifecycle
viewWillAppear 会被多次调用, 主要用于数据同步,例如离开了这个View,之后重新开启的时候,数据会重新加载,保证视图内的数据是最新的数据。
- (void)viewWillAppear:(BOOL)animated;
适用于对不可见的内容进行同步。例如一些网络同步最好在这里调用,如果在viewdidload 里面调用的话,一旦视图内容过大没有出现,则会造成网络无法加载成功。
viewWillDisappear主用用于动画的停止,释放View中的对象viewDidDisappear 释放内存,将对象置nil.awakeFromNib 是storyboard的方法,里面的函数运行在MVC被加载之前。viewWillLayoutSubviews 布局改变的时候调用,例如横竖屏切换的时候
NSNotification
添加一个通知中心:
声明一个通知中心 : [NSNotificationCenter defaultCenter]
通知中心开启监听方法:
-(void)addObserver:(id)observer // you (the object to get notified)
selector:(SEL)methodToInvokeIfSomethingHappens
name:(NSString *)name // name of station (a constant somewhere)
object:(id)sender; // whose changes you’re interested in (nil is anyone’s)
当收到广播的时候,会触发这个方法:
-(void)methodToInvokeIfSomethingHappens:(NSNotification *)notification
{
notification.name // the name passed above
notification.object // the object sending you the notification
notification.userInfo // notification-specific information about what happened
}
如果要手动发送信息的话利用:
NSString *model=@"测试";
[[NSNotificationCenter defaultCenter] postNotificationName:@"Levi"
object:model];
附上利用通知中心监听UITextView字体大小的源代码:
//
// AttributorViewController.m
// Attributor
//
// Created by YuLei on 14-6-23.
// Copyright (c) 2014年 ___DuanYuLei___. All rights reserved.
//
#import "AttributorViewController.h"
@interface AttributorViewController ()
@property (weak, nonatomic) IBOutlet UITextView *body;
@property (weak, nonatomic) IBOutlet UILabel *headline;
@property (weak, nonatomic) IBOutlet UIButton *outlineButton;
@end
@implementation AttributorViewController
- (IBAction)changeBodySelectionColorToMatchBackgroundOfButton:(UIButton *)sender {
[self.body.textStorage addAttribute:NSForegroundColorAttributeName
value:sender.backgroundColor
range:self.body.selectedRange];
}
- (IBAction)outlineBodySelection{
[self.body.textStorage addAttributes:@{NSStrokeWidthAttributeName: @-,
NSStrokeColorAttributeName: [UIColor blackColor]}
range:self.body.selectedRange];
}
- (IBAction)unoutlineBodySelection {
[self.body.textStorage removeAttribute:NSStrokeWidthAttributeName
range:self.body.selectedRange];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(preferdFontsChanged:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
- (void)preferdFontsChanged:(NSNotification *)notification
{
[self usePreferredFonts];
}
- (void)usePreferredFonts
{
self.body.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
self.headline.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self usePreferredFonts];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIContentSizeCategoryDidChangeNotification object:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:self.outlineButton.currentTitle];
[title setAttributes:@{NSStrokeWidthAttributeName: @,
NSStrokeColorAttributeName: self.outlineButton.tintColor} range:NSMakeRange(, [title length])];
[self.outlineButton setAttributedTitle:title forState:UIControlStateNormal];
}
@end
@%28%u5B66%u4E60%u7B14%u8BB0-%u6BB5%u7389%u78CA%29%5BStanford%20course%5D%0AView%20Controller%20Lifecycle%20%0A%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A-%20-%20-%0A%3E%u8FD9%u7BC7%u6587%u662F%u6211%u8BB0%u8F7D%60Developing%20iOS%207%20Apps%u516C%u5F00%u8BFE%60%20%u7B2C5%u8BFE%u7684%u7B14%u8BB0%0A%0A%23%23%23%20UITextView%0A%0A%23%23%23%23Set%20its%20text%20and%20attributes%20via%20its%20NSMutableAttributedString%0A%0A%u4F7F%u7528UITextView%20%u8981%u5C5E%u6027%60NSTextStorage%60%u7C7B%u578B%0A%0A%60%60%60%0A@property%20%28nonatomic%2C%20readonly%29%20NSTextStorage%20*textStorage%3B%0A%60%60%60%0ANSTextStorage%20%u662FNSMutableAttributedString%u7684%u4E00%u4E2A%u5B50%u7C7B%0A%u5229%u7528%60NSTextStorage%60%u66F4%u7B80%u6D01%u7684%u4FEE%u6539%60UITextView%60%u7684%u66F4%u65B0%uFF0C%u4F8B%u5982%u5B57%u4F53%u989C%u8272%u3002%0A%0A%0A%0A%23%23%23%20View%20Controller%20Lifecycle%0A%0A%60viewWillAppear%20%60%20%u4F1A%u88AB%u591A%u6B21%u8C03%u7528%2C%20%u4E3B%u8981%u7528%u4E8E%u6570%u636E%u540C%u6B65%uFF0C%u4F8B%u5982%u79BB%u5F00%u4E86%u8FD9%u4E2AView%uFF0C%u4E4B%u540E%u91CD%u65B0%u5F00%u542F%u7684%u65F6%u5019%uFF0C%u6570%u636E%u4F1A%u91CD%u65B0%u52A0%u8F7D%uFF0C%u4FDD%u8BC1%u89C6%u56FE%u5185%u7684%u6570%u636E%u662F%u6700%u65B0%u7684%u6570%u636E%u3002%0A%0A%60%60%60%0A-%20%28void%29viewWillAppear%3A%28BOOL%29animated%3B%0A%60%60%60%0A%0A%u9002%u7528%u4E8E%u5BF9%u4E0D%u53EF%u89C1%u7684%u5185%u5BB9%u8FDB%u884C%u540C%u6B65%u3002%u4F8B%u5982%u4E00%u4E9B%u7F51%u7EDC%u540C%u6B65%u6700%u597D%u5728%u8FD9%u91CC%u8C03%u7528%uFF0C%u5982%u679C%u5728viewdidload%20%u91CC%u9762%u8C03%u7528%u7684%u8BDD%uFF0C%u4E00%u65E6%u89C6%u56FE%u5185%u5BB9%u8FC7%u5927%u6CA1%u6709%u51FA%u73B0%uFF0C%u5219%u4F1A%u9020%u6210%u7F51%u7EDC%u65E0%u6CD5%u52A0%u8F7D%u6210%u529F%u3002%0A%0A%60viewWillDisappear%20%60%u4E3B%u7528%u7528%u4E8E%u52A8%u753B%u7684%u505C%u6B62%uFF0C%u91CA%u653EView%u4E2D%u7684%u5BF9%u8C61%0A%60viewDidDisappear%20%60%20%u91CA%u653E%u5185%u5B58%uFF0C%u5C06%u5BF9%u8C61%u7F6E%60nil%60.%0A%60awakeFromNib%60%20%u662Fstoryboard%u7684%u65B9%u6CD5%uFF0C%u91CC%u9762%u7684%u51FD%u6570%u8FD0%u884C%u5728MVC%u88AB%u52A0%u8F7D%u4E4B%u524D%u3002%0A%60viewWillLayoutSubviews%60%20%u5E03%u5C40%u6539%u53D8%u7684%u65F6%u5019%u8C03%u7528%uFF0C%u4F8B%u5982%u6A2A%u7AD6%u5C4F%u5207%u6362%u7684%u65F6%u5019%0A%23%23%23%20NSNotification%0A%0A%u6DFB%u52A0%u4E00%u4E2A%u901A%u77E5%u4E2D%u5FC3%uFF1A%0A%0A%u58F0%u660E%u4E00%u4E2A%u901A%u77E5%u4E2D%u5FC3%20%uFF1A%20%60%5BNSNotificationCenter%20defaultCenter%5D%60%0A%0A%u901A%u77E5%u4E2D%u5FC3%u5F00%u542F%u76D1%u542C%u65B9%u6CD5%uFF1A%0A%0A%60%60%60%0A-%28void%29addObserver%3A%28id%29observer%20//%20you%20%28the%20object%20to%20get%20notified%29%0A%20%20%20%20selector%3A%28SEL%29methodToInvokeIfSomethingHappens%0A%20%20%20%20name%3A%28NSString%20*%29name%20//%20name%20of%20station%20%28a%20constant%20somewhere%29%0A%20%20%20%20object%3A%28id%29sender%3B%20//%20whose%20changes%20you%u2019re%20interested%20in%20%28nil%20is%20anyone%u2019s%29%0A%60%60%60%0A%0A%0A%u5F53%u6536%u5230%u5E7F%u64AD%u7684%u65F6%u5019%uFF0C%u4F1A%u89E6%u53D1%u8FD9%u4E2A%u65B9%u6CD5%uFF1A%20%20%0A%0A%60%60%60%0A-%28void%29methodToInvokeIfSomethingHappens%3A%28NSNotification%20*%29notification%0A%7B%0A%20%20%20%20notification.name%20//%20the%20name%20passed%20above%0A%20%20%20%20notification.object%20//%20the%20object%20sending%20you%20the%20notification%20%0A%20%20%20%20notification.userInfo%20//%20notification-specific%20information%20about%20what%20happened%0A%7D%0A%60%60%60%0A%0A%u5982%u679C%u8981%u624B%u52A8%u53D1%u9001%u4FE1%u606F%u7684%u8BDD%u5229%u7528%uFF1A%0A%0A%60%60%60%20objectivec%0ANSString%20*model%3D@%22%u6D4B%u8BD5%22%3B%0A%5B%5BNSNotificationCenter%20defaultCenter%5D%20postNotificationName%3A@%22Levi%22%20%0Aobject%3Amodel%5D%3B%0A%60%60%60%0A%0A%u9644%u4E0A%u5229%u7528%60%u901A%u77E5%u4E2D%u5FC3%60%u76D1%u542C%60UITextView%60%u5B57%u4F53%u5927%u5C0F%u7684%u6E90%u4EE3%u7801%uFF1A%0A%0A%60%60%60%0A//%0A//%20%20AttributorViewController.m%0A//%20%20Attributor%0A//%0A//%20%20Created%20by%20YuLei%20on%2014-6-23.%0A//%20%20Copyright%20%28c%29%202014%u5E74%20___DuanYuLei___.%20All%20rights%20reserved.%0A//%0A%0A%23import%20%22AttributorViewController.h%22%0A%0A@interface%20AttributorViewController%20%28%29%0A@property%20%28weak%2C%20nonatomic%29%20IBOutlet%20UITextView%20*body%3B%0A@property%20%28weak%2C%20nonatomic%29%20IBOutlet%20UILabel%20*headline%3B%0A@property%20%28weak%2C%20nonatomic%29%20IBOutlet%20UIButton%20*outlineButton%3B%0A%0A@end%0A%0A@implementation%20AttributorViewController%0A-%20%28IBAction%29changeBodySelectionColorToMatchBackgroundOfButton%3A%28UIButton%20*%29sender%20%7B%0A%20%20%20%20%5Bself.body.textStorage%20addAttribute%3ANSForegroundColorAttributeName%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%3Asender.backgroundColor%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20range%3Aself.body.selectedRange%5D%3B%0A%7D%0A%0A-%20%28IBAction%29outlineBodySelection%7B%0A%20%20%20%20%5Bself.body.textStorage%20addAttributes%3A@%7BNSStrokeWidthAttributeName%3A%20@-3%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20NSStrokeColorAttributeName%3A%20%5BUIColor%20blackColor%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20range%3Aself.body.selectedRange%5D%3B%0A%7D%0A%0A-%20%28IBAction%29unoutlineBodySelection%20%7B%0A%20%20%20%20%5Bself.body.textStorage%20removeAttribute%3ANSStrokeWidthAttributeName%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20range%3Aself.body.selectedRange%5D%3B%0A%7D%0A%0A%0A-%20%28void%29viewWillAppear%3A%28BOOL%29animated%0A%7B%0A%20%20%20%20%5Bsuper%20viewWillAppear%3Aanimated%5D%3B%0A%20%20%20%20%5B%5BNSNotificationCenter%20defaultCenter%5D%20addObserver%3Aself%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20selector%3A@selector%28preferdFontsChanged%3A%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3AUIContentSizeCategoryDidChangeNotification%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20object%3Anil%5D%3B%0A%7D%0A%0A-%20%28void%29preferdFontsChanged%3A%28NSNotification%20*%29notification%0A%7B%0A%20%20%20%20%5Bself%20usePreferredFonts%5D%3B%0A%7D%0A%0A-%20%28void%29usePreferredFonts%0A%7B%0A%20%20%20%20self.body.font%20%3D%20%5BUIFont%20preferredFontForTextStyle%3AUIFontTextStyleBody%5D%3B%0A%20%20%20%20self.headline.font%20%3D%20%5BUIFont%20preferredFontForTextStyle%3AUIFontTextStyleHeadline%5D%3B%0A%7D%0A%0A-%20%28void%29viewWillDisappear%3A%28BOOL%29animated%0A%7B%0A%20%20%20%20%5Bsuper%20viewWillDisappear%3Aanimated%5D%3B%0A%20%20%20%20%5Bself%20usePreferredFonts%5D%3B%0A%20%20%20%20%5B%5BNSNotificationCenter%20defaultCenter%5D%20removeObserver%3Aself%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3AUIContentSizeCategoryDidChangeNotification%20object%3Anil%5D%3B%0A%7D%0A%0A%0A-%20%28void%29viewDidLoad%0A%7B%0A%20%20%20%20%5Bsuper%20viewDidLoad%5D%3B%0A%09//%20Do%20any%20additional%20setup%20after%20loading%20the%20view%2C%20typically%20from%20a%20nib.%0A%20%20%20%20NSMutableAttributedString%20*title%20%3D%20%5B%5BNSMutableAttributedString%20alloc%5D%20initWithString%3Aself.outlineButton.currentTitle%5D%3B%0A%20%20%20%20%5Btitle%20setAttributes%3A@%7BNSStrokeWidthAttributeName%3A%20@3%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20NSStrokeColorAttributeName%3A%20self.outlineButton.tintColor%7D%20range%3ANSMakeRange%280%2C%20%5Btitle%20length%5D%29%5D%3B%0A%20%20%20%20%5Bself.outlineButton%20setAttributedTitle%3Atitle%20forState%3AUIControlStateNormal%5D%3B%0A%7D%0A%0A%0A@end%0A%0A%60%60%60
iOS生命周期 & 通知中心的更多相关文章
- 从实践谈iOS生命周期
从实践谈iOS生命周期 个人感觉生命周期无论在Android,还是iOS都是很重要的概念,因为在每个声明周期的状态下我们可以做很多预加载或者处理的操作.因此在这里主要总结下ViewController ...
- iOS 生命周期
应用生命周期 App启动:当App启动时,首先由not running状态切换到inactive状态,此时调用application:didFinishLaunchingWithOptions: ...
- iOS开发之通知中心(NSNotificationCenter)
前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...
- IOS NSNotification Center 通知中心的使用
通知中心,它是IOS程序内部的一种消息广播机制,通过它,可以实现无引用关系的对象之间的通信.通知中心他是基于观察者模式,它只能进行程序内部通信,不能跨应用程序进程通信.当通知中心接受到消息后会根据设置 ...
- iOS 生命周期 -init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear、viewDidDisappear 区别和用途
iOS视图控制对象生命周期-init.viewDidLoad.viewWillAppear.viewDidAppear.viewWillDisappear.viewDidDisappear的区别及用途 ...
- iOS之NSNotificationCenter通知中心使用事项
其实这里的通知和之前说到的KVO功能很想,也是用于监听操作的,但是和KVO不同的是,KVO只用来监听属性值的变化,这个发送监听的操作是系统控制的,我们控制不了,我们只能控制监听操作,类似于Androi ...
- iOS 设计模式-NSNotificationCenter 通知中心
通知介绍 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发布通知(NSNotification),描述 ...
- iOS生命周期
1.application didFinishLaunchingWithOptions:当应用程序启动时执行,应用程序启动入口,只在应用程序启动时执行一次.若用户直接启动,lauchOptions内无 ...
- 生命周期-初识IOS
经常因为生命周期的事情,而视图顺序加载错误,或者出现一系列的小错误并且修改不出来,程序员不知道生命周期确实挺可悲的. IOS生命周期: 自上而下的执行,并且viewDidLoad只会执行一次,所以我们 ...
随机推荐
- TextView——setCompoundDrawables说明
Drawable drawable = mContext.getResources().getDrawable(R.drawable.duringtime); drawable.setBounds( ...
- js 通信
js 页面间的通信 看了一下公司原来的代码,原页面ajax post返回一个页面完整的HTML,然后再打开一个新页面并输出ajax返回的所有代码到新页面上,在新页面上以表单提交的形式实现重定向. 任凭 ...
- Python开发一个csv比较功能相关知识点汇总及demo
Python 2.7 csv.reader(csvfile, dialect='excel', **fmtparams)的一个坑:csvfile被csv.reader生成的iterator,在遍历每二 ...
- WebAPI 和 WebService的区别
webapi用的是http协议,webservice用的是soap协议 webapi无状态,相对webservice更轻量级.webapi支持如get,post等http操作 http soap关系 ...
- C#操作Xml:使用XmlReader读Xml
XmlDocument和XElement在读取Xml时要将整个Xml文档放到内存中去操作,这样做操作简单,但是很费内存和IO(可能是磁盘IO或者网络IO):而在有些场景下我们必须考虑尽可能节省内存和I ...
- 【百度地图API】如何用圆形搜索获取中心点周围100米内全部关键点?如天安门附近所有的餐厅、加油站、宾馆、大厦等
原文:[百度地图API]如何用圆形搜索获取中心点周围100米内全部关键点?如天安门附近所有的餐厅.加油站.宾馆.大厦等 摘要: 在LBS上有这样一个常用的功能,查找附近所有的关键点(POI点,比如标志 ...
- ssh 自动登录
工作中经常会有这样的需求场景,因为要在其它电脑上做操作, 需要从PC A ssh 到 PC B,PC A 可能是自己的工作机,PC B 可能是服务器.一般会使用 SSH 登录到 server 上再进行 ...
- JS中5秒中跳转到其他页面
原文:JS中5秒中跳转到其他页面 <head> <meta http-equiv="Content-Type" content="text/html; ...
- HBase数据同步ElasticSearch该程序
ElasticSearch的River机械 ElasticSearch本身就提供了River机械,对于同步数据. 在这里,现在能找到的官方推荐River: http://www.elasticsear ...
- ASP.NET MVC基于标注特性的Model验证:一个Model,多种验证规则
原文:ASP.NET MVC基于标注特性的Model验证:一个Model,多种验证规则 对于Model验证,理想的设计应该是场景驱动的,而不是Model(类型)驱动的,也就是对于同一个Model对象, ...