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只会执行一次,所以我们 ...
随机推荐
- Lichee (六) 优化配置的微内核
我们的分析<Lichee(二) 在sun4i_crane平台下的编译 >的时候.竟然没有一个步骤是在配置内核 make ARCH=arm menuconfig 细致的读过的代码的会发现,在 ...
- Spring4 SpringMVC Hibernate4 Freemaker 集成示例
变更更正(2014-05-30 13:47:22):一些IDE在web.xml我们会报告这个错误: cvc-complex-type.2.4.a: Invalid content was found ...
- 我学的是设计模式的视频教程——装饰图案,装饰图案VS代理模式
课程视频 装饰模式 装饰模式VS代理模式1 装饰模式VS代理模式2 课程笔记 课程笔记 课程代码 课程代码 新课程火热报名中 课程介绍 版权声明:本文博主原创文章,博客,未经同意不得转载.
- 第6章 适配器模式(Adapter Pattern)
原文 第6章 适配器模式(Adapter Pattern) 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 解决的问 ...
- crawler_java应用集锦9:httpclient4.2.2的几个常用方法,登录之后访问页面问题,下载文件_设置代理
在工作中要用到android,然后进行网络请求的时候,打算使用httpClient. 总结一下httpClient的一些基本使用. 版本是4.2.2. 使用这个版本的过程中,百度很多,结果都是出现的o ...
- 【设计模式】Abstract Factory模式
抽象工厂模式是工厂方法模式的进一步强化.当工厂函数仅仅须要产生一种类型的产品(全部产品都继承自同一抽象基类)时,使用工厂方法模式就可以. 可是.当用户程序须要创建多种类型的产品,而这些产品又有一定的内 ...
- RPC和RMI的区别(Difference Between RPC and RMI)
RPC和RMI的区别(Difference Between RPC and RMI) RPC vs RMI RPC (Remote Procedure Call) and RMI (Remote Me ...
- Android结构分析Android智能指针(两)
笔者:刘蒿羽 博客:http://blog.csdn.net/liuhaoyutz Android版本号:4.4.2 在上一篇文章中,我们分析了Android智能指针中的强指针sp,本文我们来分析弱指 ...
- 蜗牛—Hibernate之初识配置
下载Hibernate的jar包 把下面jar文件考到项目lib下 然后在myeclipse中打开database的view视图创建一个新的数据库连接 接下来,配置连接,须要导入ORACLE的jar包 ...
- C#5.0新特性
C#5.0新特性 C#5.0最大的新特性,莫过于Async和Parallel. 以往我们为了让用户界面保持相应,我们可以直接使用异步委托或是System.Threading命名空间中的成员,但Syst ...