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: @-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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。