iOS编程第四版第六章---UIViewController

本章注意点如下:

1. 在堆内存里的对象需要指针指向它。在栈内存里的对象不需要指针指向它。


2. The rule is: if you create a Core
Graphics object with a function that has the word Create or Copy in it, you must call the matching
Release function and pass a pointer to the object as the first argument.


3. P114
class extension
可见性。别的类包括子类和子类的实例是无法看到class extension 中的内容的。
也可以把class extension 放在单独的头文件中, 然后导入到implementation文件中。


4. P121
A view controller’s view is not created until it needs to appear on the screen. This optimization is
called lazy loading, and it can often conserve memory and improve performance.


5. P128
So if you want to connect to the object that loads the NIB at runtime, you connect to the File's Owner
when working in the XIB. The first step is to tell the XIB file that the File's Owner is going to be an
instance of BNRReminderViewController.


5. P135
A local notification is
a way for an application to alert the user even when the application is not currently running.
(An application can also use push notifications that are implemented using a backend server. For more
about push notifications, read Apple’s Local and Push Notification Programming Guide.)


6. P137
Build and run the application. The console reports that BNRHypnosisViewController loaded its view
right away. Tap BNRReminderViewController’s tab, and the console will report that its view is now
loaded. At this point, both views have been loaded, so switching between the tabs now will no longer
trigger the viewDidLoad method. (Try it and see.)


!!!!To preserve the benefits of lazy loading, you should never access the view property of a view controller
in initWithNibName:bundle:. Asking for the view in the initializer will cause the view controller to
load its view prematurely.


7. P137 基于懒加载,什么时候如何访问子类呢?
So where can you access a subview? There are two main options, depending on what you need to
do. The first option is the viewDidLoad method that you overrode to spot lazy loading. The view
controller receives this message after the view controller’s NIB file is loaded, at which point all of the
view controller’s pointers will be pointing to the appropriate objects. The second option is another
UIViewController method viewWillAppear:. The view controller receives this message just before its
view is added to the window.


What is the difference? You override viewDidLoad if the configuration only needs to be done once
during the run of the app. You override viewWillAppear: if you need the configuration to be done and
redone every time the view controller appears on screen.


8. P138 
Interacting with View Controllers and Their Views

Let’s look at some methods that are called during the lifecycle of a view controller and its view. Some
of these methods you have already seen, and some are new:

• application:didFinishLaunchingWithOptions: is where you instantiate and set an
application’s root view controller.
This method gets called exactly once when the application has launched. Even if you go to
another app and come back, this method does not get called again. If you reboot your phone and
start the app again, application:didFinishLaunchingWithOptions: will get called again.

• initWithNibName:bundle: is the designated initializer for UIViewController.
When a view controller instance is created, its initWithNibName:bundle: gets called once. Note
that in some apps, you may end up creating several instances of the same view controller class.
This method will get called once on each as it is created.

• loadView: is overridden to create a view controller’s view programmatically.

• viewDidLoad can be overridden to configure views created by loading a NIB file. This method
gets called after the view of a view controller is created.

• viewWillAppear: can be overridden to configure views created by loading a NIB file.
This method and viewDidAppear: will get called every time your view controller is moved on
screen. viewWillDisappear: and viewDidDisappear: will get called every time your view
controller is moved offscreen. So if you launch the app you are working on and hop back and
forth between Hypnosis and Reminder, BNRReminderViewController’s viewDidLoad method will
be called once, but viewWillAppear: will be called dozens of times.


9. P138 
KVC
key-value coding
When a NIB file is read in, the outlets are set using a mechanism called Key-value coding (or KVC).
Key-value coding is a set of methods defined in NSObject that enable you to set and get the values of
properties by name. Here are two of the methods:
- (id)valueForKey:(NSString *)k;
- (void)setValue:(id)v forKey:(NSString *)k;


记住:IBAction名千万别使用setXXX,因为那会跟系统给的setter方法重名。
IBoutlet也是一样。不要使用xxx作为名字。


10. P140
Retina 显示和非Retina显示


The only solution is to bundle two image files with
your application: one at a pixel resolution equal to the number of points on the screen for non-Retina
displays and one twice that size in pixels for Retina displays.


Fortunately, you do not have to write any extra code to handle which image gets loaded on which
device. All you have to do is suffix the higher-resolution image with @2x. Then, when you use
UIImage’s imageNamed: method to load the image, this method looks in the bundle and gets the file that
is appropriate for the particular device.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值