笔记内容

学习笔记-段玉磊 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生命周期 & 通知中心的更多相关文章

  1. 从实践谈iOS生命周期

    从实践谈iOS生命周期 个人感觉生命周期无论在Android,还是iOS都是很重要的概念,因为在每个声明周期的状态下我们可以做很多预加载或者处理的操作.因此在这里主要总结下ViewController ...

  2. iOS 生命周期

       应用生命周期 App启动:当App启动时,首先由not running状态切换到inactive状态,此时调用application:didFinishLaunchingWithOptions: ...

  3. iOS开发之通知中心(NSNotificationCenter)

    前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...

  4. IOS NSNotification Center 通知中心的使用

    通知中心,它是IOS程序内部的一种消息广播机制,通过它,可以实现无引用关系的对象之间的通信.通知中心他是基于观察者模式,它只能进行程序内部通信,不能跨应用程序进程通信.当通知中心接受到消息后会根据设置 ...

  5. iOS 生命周期 -init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear、viewDidDisappear 区别和用途

    iOS视图控制对象生命周期-init.viewDidLoad.viewWillAppear.viewDidAppear.viewWillDisappear.viewDidDisappear的区别及用途 ...

  6. iOS之NSNotificationCenter通知中心使用事项

    其实这里的通知和之前说到的KVO功能很想,也是用于监听操作的,但是和KVO不同的是,KVO只用来监听属性值的变化,这个发送监听的操作是系统控制的,我们控制不了,我们只能控制监听操作,类似于Androi ...

  7. iOS 设计模式-NSNotificationCenter 通知中心

    通知介绍 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发布通知(NSNotification),描述 ...

  8. iOS生命周期

    1.application didFinishLaunchingWithOptions:当应用程序启动时执行,应用程序启动入口,只在应用程序启动时执行一次.若用户直接启动,lauchOptions内无 ...

  9. 生命周期-初识IOS

    经常因为生命周期的事情,而视图顺序加载错误,或者出现一系列的小错误并且修改不出来,程序员不知道生命周期确实挺可悲的. IOS生命周期: 自上而下的执行,并且viewDidLoad只会执行一次,所以我们 ...

随机推荐

  1. Service与Activity与交流AIDL

    深圳旅游月.终于回来了,做了很多个月,这些天来的东西会慢慢总结出来的.今天,我们正在谈论的Service小东西:沟通. 固定通信的做法比较,基本上按照写模板可以实现. 1.Service与Activi ...

  2. 详细解释VB连接access几种方法数据库

    在VB中,连接ACCESS数据库的方法主要有以下三种 使用ADO对象,通过编写代码訪问数据库 Connection 对象 ODBC数据源 使用ADO Data 控件高速创建数据库连接 有三种连接方法 ...

  3. CodeIgniter入门——HelloWorld

    原文:CodeIgniter入门--HelloWorld CodeIgniter(CI)是一套给PHP网站开发者使用的应用程序开发框架和工具包. 初次接触,来一个HelloWorld~~~ ^_^ 准 ...

  4. http headers

    什么是HTTP Headers HTTP是“Hypertext Transfer Protocol”的所写,整个万维网都在使用这种协议,几乎你在浏览器里看到的大部分内容都是通过http协议来传输的,比 ...

  5. elasticsearch的rest搜索--- 查询

    目录: 一.针对这次装B 的解释 二.下载,安装插件elasticsearch-1.7.0   三.索引的mapping 四. 查询 五.对于相关度的大牛的文档 四. 查询 1. 查询的官网的文档   ...

  6. Hadoop-2.4.1学习之Writable及事实上现

    Hadoop基于DataInput和DataOutput实现了简单.高效的序列化协议,而Writable接口定义了Hadoop序列化的方法,MapReduce框架中的不论什么键值类型都实现了该接口,比 ...

  7. C++语言笔记系列之十二——C++的继承

    C++的继承 1.继承方式 public(公有继承) 派生类中的成员能够訪问基类的public成员和protected成员,但不能訪问基类的private成员. 派生类的对象仅仅能訪问基类的publi ...

  8. Quartz使用-入门使用(java定时任务实现)

    注:这里使用的是Quartz1.6.5版本号(包:quartz-1.6.5.jar) //測试main函数 //QuartzTest.java package quartzPackage; impor ...

  9. jQuery基础---Ajax进阶

    原文:jQuery基础---Ajax进阶 内容提纲: 1.加载请求 2.错误处理 3.请求全局事件 4.JSON 和 JSONP 5.jqXHR 对象 发文不易,转载请注明出处! 在 Ajax 基础一 ...

  10. UVA 11525 Permutation(树状数组)

    题目意思是说  给你一个数k  然后有k个si   问你1--k 的第n个全排列是多少   注意是 1 2 3...k的全排列 不是si的 N=   由观察得知(k-i)!就是k-i个数字的全排列种数 ...