[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture5

1.There is a class called NSNotificationCenter .It has a class method called defaultCenter.That returns a shared instance kind of like NSUserDefault,standard UserDefault did — a shared instance.That’s the object you use to tune into radio stations. And you do it by sending it is this message:addObserver selector name object.The first argument observe,that is,the object that wants to listen to the radio station.So in your controller — because controllers are the most common radio station listeners — this would probably just be self.This is somewhere in your controller code.Selector is the method inside of the observer that you want to be called when something appears on the radio station.Some broadcast happens.Name is the name of the radio station.And sender there — object sender —that’s if you only want to listen to radio station broadcasts that come from a certain other object.Often you pass nil here which means if anyone broadcasts on that frequency,i want to hear it.(53:00)

2.So another thing to understand is when you’re done listening,tune out.And you do that by sending a message to the notification center saying”removeObserver”.And you can remove yourself as an observer of all radio station with the first one or you can just remove yourself from listening to certain radio stations,by specifying the name of the radio station and who the sender is you don’t want to listen to anymore.It’s important to do this because,unfortunately,the notification center keeps a pointer to you that is called”Unsafe retained”.So it’s not strong or weak;it’s unsafe retained. And what unsafe retained means is that if you go out of the heap without calling this first,the notification center might try and send you a notification and crash your app.(57:00)

3.UITextView:Like UILabel,but multi-line,selectable/editabel,scrollable,etc.Set its text and attributes via its NSMutableAttributedString.Obtain the NSMutableAttributedString representing the text in the UITextView using…

@property(nonatomic,readonly)NSTextStorage *textStorage;NSTextStorage is a subclass of NSMutableAttributedString.

4.The start of the lifecycle is creation.MVCs are most often instantiated out of a storyboard.What then?

Outlet setting — Appearing and disappearing — Geometry changes — Low-memory situations.

5.After instantiation and outlet-setting,viewDidLoad is called.This is an exceptionally good place to put a lot of setup code.But be careful because the geometry of your view is not set yet!At this point,you can’t be sure you’re on an iPhone 5-sized screen or an iPad or???So do not initialize things that are geometry-dependent here.

6.Just before the view appears on screen,you get notified(argument is just whether you are appearing instantly or over time via animation)

-(void)viewWillAppear:(BOOL)animated;

Your view will only get “loaded” once,but it might appear and disappear a lot.So don’t put something in this method that really wants to be in viewDidLoad.Otherwise,you might be doing something over and over unnecessarily.

Do something here if things you display are changing while your MVC is off-screen.View’s geometry is set here,but there are other(better?)places to react to geometry.

7.And you get notified when you will disappear off screen too.This is where you put “remember what’s going on” and cleanup code.

-(void)viewWillDisappear:(BOOL)animated{
     [super viewWillDisappear:animated];//call   super in all the viewWill/Did.. methods
//let’s be nice to the user and remember the scroll position they were at
[self rememberScrollPosition];
//do some other clean up now that we’ve been removed from screen
[self saveDataToPermanentStore];
//but be careful not to do anything time-consuming here,or app will be sluggish
}

There are “did” versions of both of the appearance methods

-(void)viewDidAppear:(BOOL)animated;
-(void)viewDidDisappear:(BOOL)animated;

8.In low-memory situations,didReceiveMemoryWaring gets called…Anything “big” that can be recreated should probably be released(i.e. set strong pointer to nil).

9.Notifications:The “radio station”from the MVC slides.

NSNotificationCenter:Get the default”notification center”via[NSNotificationCenter defaultCenter]

Then send it the following message if you want to “listen to a radio station”:

-(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 instertsted in(nil is anyone‘s)
//You will then be notified when there are broadcasts
-(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
}
 

10.Be sure to “tune out”when done listening

[center removeObserver:self];or

[center removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil];

Failure to remove yourself can sometimes result in crashers.

This is because the NSNotificationCenter keeps an “unsafe retained”pointer to you.

A good place to remove yourself is when your MVC’s View goes off screen.

Or you can remove yourself in a method called dealloc(called when you leave the heap)

-(void)dealloc{

//be careful in this method!can’t access properties!you are almost gone from heap/

[[NSNotificationCenter defaultCenter]removeObserver:self];

}

时间: 2024-10-09 10:40:19

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture5的相关文章

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture7

1.One is CGFloat.It’s a floating point number.All floating point numbers that have to do with drawing on the screen or getting touch events or whatever are CGFloats.This might be a double.It might be just a regular floating point number.Not only usin

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture4

1.All objects in an array are held onto strongly in the heap.So as long as that array itself is in the heap,as long as someone has a strong pointer to the array itself,all the objects the are in the array will stay in the heap as well.Because it has

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture6

1.Abstract means that this class cannot be instantiated and used,it’s only useful as a superclass for other classes that are concrete. (04:00) 2.And I also like to document,even on my implementation any methods that basically are abstract.Any method

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture1

1.The difference is card.h is the public API.That’s what your dot h is.It’s your public API.It’s what methods in your class you want to make public so that other people can call them.Card.m is your private API and all your implementation. 2.It’s impo

机器人学-笔记-斯坦福大学公开课-class 2

kinematics 1. manipulator is defined by a set of links connected trough joints. 2. joint type: 移动,转动 3. configuration parameters 4. constraints, freedom. 5. planning motions in configuraiton coordinates 6. degress of redundancy. 7. rotation matrix.

机器人学-笔记-斯坦福大学公开课-class 1

This course is going to really cover the foundations of robotics(mathematical models). 模型的运动学特性, 驱动系统actuate the system, motors, 正确的转矩让robort move, 1. if you want to control the robot, you need to know its position -- sensor(GPS(x,y), encoders(one fr

斯坦福大学公开课:iOS 7应用开发 笔记

2015-07-06 第一讲   课务.iOS概述 -------------------------------------------------- 开始学习斯坦福大学公开课:iOS 7应用开发留下笔记

《斯坦福大学公开课:编程方法学》随笔

这是观看网易公开课 Mehran Sahami教授的<斯坦福大学公开课:编程方法学>后的随笔. 目前只看到第三次课,<Karel与Java>,我的收获有以下要点: 1.软件工程跟普通的写代码是不同的.软件应该考虑很多东西,比如:可移植性.便于升级维护等等,而不仅仅是写出代码实现功能那么简单. 2.代码是写给机器执行的,但更重要的是人要让人能看懂.(代码后期维护等等的工作量或者时间是写代码的10倍及更多,所以让代码更规范更易被人读懂很重要) 3.准确定义一个函数.一个类.一个包的功能

斯坦福大学公开课:iPad和iPhone应用开发(iOS5) 学习笔记 2

继续学习公开课 第二节课做了一个简单的计算器作为例子.大概Touch了如下知识点: 讲解了XCode4,我看了一下最新下载的是XCode8了. XCode创建工程, singleViewApplication还是有的,界面对比起XCode4来,更简洁些了,操作跟视频讲解里的差不多. 体会了下第一节课讲的MVC View的代码看不到这个感觉不太爽,特别是前面操作是将number的button拷贝到了 operation的 button,结构导致operation button也都连接到了digit