MVC:
Model = What your application is (but not how it is displayed) — UI independent (独立于UI的)
Controller =
How your Model is presented to the user (UI logic)
View = Your
Controller’s minions
Controllers can always talk directly to their
Model.
Controllers can also talk directly to their
View.
The Model and View should never speak to each other.
Can the View speak to its
Controller? — Sort of. Communication is “blind” and structured. The
Controller can drop a target on itself. Then hand out an action to the
View. The View sends the action when things happen in the UI.
Sometimes the View needs to synchronize with the
Controller (will, did, should...). The
Controller sets itself as the
View’s delegate. The delegate is set via a protocol (i.e. it’s “blind” to class).
Views do not own the data they display. So, if needed, they have a protocol to acquire it.
Controllers are almost always that data source (not
Model!). Controllers interpret/format
Model information for the
View.
Can the Model talk directly to the
Controller? — No. The Model is (should be) UI independent.
So what if the Model has information to update or something? — It uses a “radio station” -like broadcast mechanism (Notification & KVO (Key Value Observing)).
Controllers (or other
Model) “tune in” to interesting stuff.
A View might “tune in”, but probably not to a
Model’s “station”.
A big application might have single, shared
Model.
Objective-C:
Objective-C is a strict superset of C, so everything you can do in C, you can do in objective-C.
A property is basically a setter method and a getter method.
.h is the public API. — 需要指定父类
.m is your private API and all your implementation. — 不需要指定父类
If you have specified your superclass, you have to import it:
|
#import
<Foundation/NSObject.h>
|
However, when our superclass is something that’s in iOS, we don’t usually just import that class’s header file, we actually import the entire framework:
|
#import
<Foundation/Foundation.h>
|
对象前面的 * :
In objective-C, all objectives, live in a heap (堆), and we have pointers to them.
强指针,弱指针,nil:
只要有强指针指向该内存,将内存中的对象留在该内存中。只要不再有强指针指向它,内存就会释放。如果这个指针是弱指针,会被设置为nil。
在objective-C中,你可以发送消息给nil指针,如果你将消息发送给nil指针,它不会执行任何代码。如果那个消息有返回值,它会返回0.
nonatomic(非原子性):
nonatomic means calling this setter and getter that go along with this property is not thread safe. So you can’t have two threads trying to set this property at the same time.
另外,如果没有nonatomic,会有锁定代码(locking code),即setter和getter的实现方法。
BOOL类型的属性不需要strong或者weak来修饰。只需要写成:
@property (nonatomic) BOOL chosen;
因为它是原始类型(primitive type),它没有存储在堆中
本文深入解析了MVC架构的概念及其在UI独立性、控制器与视图之间的交互方式,同时介绍了Objective-C语言的基本特性,如属性、强指针、弱指针和nonatomic属性,以及其在应用开发中的实际应用。
843

被折叠的 条评论
为什么被折叠?



