转载

(5/18)重学Standford_iOS7开发_视图控制器生命周期_课程笔记

第五课:

1、UITextView

@property (nonatomic, readonly) NSTextStorage *textStorage;//注意为只读属性,因此不能直接更改内容,NSTextStorage为NSMutableAttributeString的子类,因此可以更改字符串属性内容(而非字符串) //例如,添加属性 [self.body.textStorage addAttribute:NSForegroundColorAttributeName  value:sender.backgroundColor  range:self.body.selectedRange]; @property (nonatomic, strong) UIFont *font; //例如添加字体 self.body.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];     self.headLine.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; //文本布局功能,如图文混排等 @property (readonly) NSTextContainer *textContainer; @property (readonly) NSLayoutManager *layoutManager; 

2、View Controller Lifecycle

- (instancetype)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle;//非storyboard加载方式,指定初始化器 - (void)awakeFromNib;//从storyboard中加载UI时调用,此时未设置输出口  //设置输出口  - (void)viewDidLoad;//可以放置控制器初始化代码(一次性),如init。不可放置与几何相关的代码,此时控制器中的UI边界未确定  //确定几何布局 //当视图frame发生变化时就会被调用,此处适合添加与几何相关的代码 - (void)viewWillLayoutSubviews; - (void)viewDidLayoutSubviews; //其他关于屏幕旋转等API...  - (void)viewWillAppear:(BOOL)animated;//生命周期中可能会被反复调用,因此不能放置一次性初始化内容 - (void)viewDidAppear:(BOOL)animated; - (void)viewWillDisappear:(BOOL)animated;//可以进行一些当前数据保存工作 - (void)viewDidDisappear:(BOOL)animated;  - (void)didReceiveMemoryWarning;//系统在内存不足时调用

3、NSNotification

(本节课只涉及到如何收听通知)

[NSNotificationCenter defaultCenter];//获取defaultCenter //添加观察者 - (void)addObserver:(id)observer //接收通知的对象       selector:(SEL)methodToInvokeIfSomethingHappens       name:(NSString *)name //广播名      object:(id)sender; //监听的广播对象,nil指监听所有对象 //接收到通知后调用方法 - (void)methodToInvokeIfSomethingHappens:(NSNotification *)notification {  notification.name // the name passed above  notification.object //发送广播的对象  notification.userInfo // notification-specific information about what happened(取决于发送者) } //结束收听通知,通常放于视图从界面消失时 [center removeObserver:self]; [center removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil]; 

通知实例(系统设置字体改变)

//视图出现在屏幕上注册通知 - (void)viewWillAppear:(BOOL)animated {  [super viewWillAppear:animated];  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferredFontChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil]; } //通知出现时调用方法 - (void)preferredFontChanged:(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];  [[NSNotificationCenter defaultCenter] removeObserver:self              name:UIContentSizeCategoryDidChangeNotification               object:nil]; 

4、作业

课程示例Attributor源码: https://github.com/NSLogMeng/Stanford_iOS7_Study/commit/242826c2220afe978bc1d060c2dff19578a835c9

课程视频地址:网易公开课: http://open.163.com/movie/2014/1/L/H/M9H7S9F1H_M9H801GLH.html

或者iTunes U搜索standford课程

正文到此结束
Loading...