码迷,mamicode.com
首页 > 移动开发 > 详细

iOS生命周期 & 通知中心

时间:2014-06-25 19:04:52      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   code   http   tar   

笔记内容

学习笔记-段玉磊 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: @-3,
                                           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: @3,
                           NSStrokeColorAttributeName: self.outlineButton.tintColor} range:NSMakeRange(0, [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生命周期 & 通知中心,布布扣,bubuko.com

iOS生命周期 & 通知中心

标签:des   style   class   code   http   tar   

原文地址:http://www.cnblogs.com/firstrate/p/3805865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!