笔记内容

学习笔记-段玉磊 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. zoj3791(An Easy Game) DP

    意甲冠军:给定两个01弦s1,s2.每一个变化s1在m字 - 位.要求k制作步骤之后s1变s2有多少种方法. 解法:DP,关键是状态的设计.考虑还是唯一性和可传递性.dp[i][j]表示第i步后有j个 ...

  2. 配置静态监听解决ORA-12514错误的案例(转)

    今天做Linux下DG配置的时候,遇到一个现象,tnsname.ora文件配置都正常,tnsping也正常,监听也正常,但是仍然报ORA-12514错误:   SQL> set lin 130 ...

  3. cocos2d-x 发动机分析:程序如何开始和结束?

    原创地址:http://game.dapps.net/gamedev/game-engine/9515.html 感谢原创分享! 怎么样使用 Cocos2d-x 高速开发游戏,方法非常easy,你能够 ...

  4. poj 1160 Post Office (间隔DP)

    Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15966   Accepted: 8671 Desc ...

  5. react学习笔记1--基础知识

    什么是react A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES[React是一个用于构建用户界面的JavaScript库.] React之所以快, ...

  6. MySQL中游标使用以及读取文本数据

    原文:MySQL中游标使用以及读取文本数据 前言 之前一直没有接触数据库的学习,只是本科时候修了一本数据库基本知识的课.当时只对C++感兴趣,天真的认为其它的课都没有用,数据库也是半懂不懂,胡乱就考试 ...

  7. java_软件发布版本_Asynch HttpClien 对比发行版本说明_Alpha、Beta、RC、GA版本的区别

    今天看了一天的apache 的httpasyncclient ,为了和默认的httpclient做个对比,httpcore是核心轻量级的提供传统阻塞IO 和 java NIO两种方式,httpclie ...

  8. 隐藏Console形式无效(继续1)

    [2014/10/19  23:57 ] :由port主机遥控. 该程序的执行后,计划自己主动开放之机999port,其他计算机将能够通过999port机器的操作. 程序中使用的到的命令: telne ...

  9. A WebBrowser Toy

    原文:A WebBrowser Toy 记得上大学时,某老师为了防止学生上课不听讲,只准学生在课堂上看他放映的PPT,不准学生拷贝,而考试的内容恰恰是PPT上的内容,于是一个同学来找我,我就用VB写了 ...

  10. ASP.NET MVC 插件化

    ASP.NET MVC 插件化机制 2015-03-14 22:25 by 杨康新, 1328 阅读, 15 评论, 收藏, 编辑 概述 nopCommerce的插件机制的核心是使用BuildMana ...