본문 바로가기

Programming/iPhone, Xcode

iOS 개념정리 - Scroll View, NSSortDescriptor, Notifications, Table View Editing


iOS 를 공부하면 할 수록 재미있다.
더욱이 4년 가량 Console Application 만 개발하는 필자로서는 iPhone 개발이 개발과 동시에 테스트 및 실행이 바로 눈에 보여지기 때문에 정말 재미있다.

오늘은 새롭게 배운 개념들을 정리하기 위해서 글을 포스팅 해볼까 한다. (Head First iPhone & iPad 개발 이라는 책에서 개념 설명들을 발췌하였고, iOS Reference 에서도 발췌하였다.)
 

항상 생각하는 것이지만, 배운 것을 글로 쓴다는 것은 기억력 향상에 도움을 준다.
이는 몸소 느끼는 것이다. :)

1. Scroll View - 렌즈와 같은 일을 수행하는데, 뭔가 하냐면, 필요한 View 만 보여주고, 다른 것은 스크롤시키는 기능을 한다. 즉, 앱에서 보여주어야 할 내용이 많은데, 그것을 다 보여줄 수 없기 때문에 관심 있는 것만 보여주고, 다른 것들은 스크롤을 이용하여 보이지 않게 하는 것이다. Scroll View 의 두 가지 중요한 일이 있는데, 아래와 같다.

    * 사용자가 보여주고자 하는 범위를 drag 할수 있도록 한다.
    * 손가락을 이용하여 해당범위를 축소/확대 하는 기능을 제공한다.
 
아래 그림을 보면 쉽게 이해할 수 있다.

왼쪽 그림은 drag 하는 모습.



2. NSSortDescriptor  - 객체를 정렬하기 위해 사용하는 클래스이다. 주로 배열을 정렬하는데 사용하는데, 내림차순, 오름차순으로 정렬을 할 수 있고, 정렬을 하고자 하는 배열, 혹은 property 들의 key path 를 받아서 정렬을 수행한다.

간단한 코드 샘플을 보면 아래와 같이 사용할 수 있음을 알 수 있다.

// Create the sort descriptors array.

NSSortDescriptor *authorDescriptor = [[NSSortDescriptor alloc] initWithKey:@"author" ascending:YES];

NSSortDescriptor *titleDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor, titleDescriptor, nil]; 

iOS Reference 를 보면 아래와 같다.

initWithKey:ascending:comparator:

Returns an NSSortDescriptor object initialized to do with the given ordering and comparator block.

- (id)initWithKey:(NSString *)key ascending:(BOOL)ascending comparator:(NSComparator)cmptr

Parameters

key

The property key to use when performing a comparison. In the comparison, the property is accessed using key-value coding (see Key-Value Coding Programming Guide).

ascending

YES if the receiver specifies sorting in ascending order, otherwise NO.

cmptr

A comparator block.

Return Value

An NSSortDescriptor initialized with the specified key, ordering and comparator.


3. Notifications - 우리가 이용하고, 모니터링 할 수 있는 시스템수준의 이벤트들이다. default notification center 는 대부분의 알림을 처리한다. 서로다른 프레임워크는 서로다른 notifications 를 사용한다. 물론 사용자 정의 notifications 를 생성하는 것도 가능하다.

iOS Reference 에서 설명된 것을 보자.

A notification is a message sent to one or more observing objects to inform them of an event in a program. The notification mechanism of Cocoa follows a broadcast model. It is a way for an object that initiates or handles a program event to communicate with any number of objects that want to know about that event. These recipients of the notification, known as observers, can adjust their own appearance, behavior, and state in response to the event. The object sending (or posting) the notification doesn’t have to know what those observers are. Notification is thus a powerful mechanism for attaining coordination and cohesion in a program. It reduces the need for strong dependencies between objects in a program (such dependencies would reduce the reusability of those objects). Many classes of the Foundation, AppKit, and other Objective-C frameworks define notifications that your program can register to observe.

The centerpiece of the notification mechanism is a per-process singleton object known as the notification center (NSNotificationCenter). When an object posts a notification, it goes to the notification center, which acts as a kind of clearing house and broadcast center for notifications. Objects that need to know about an event elsewhere in the application register with the notification center to let it know they want to be notified when that event happens. Although the notification center delivers a notification to its observers synchronously, you can post notifications asynchronously using a notification queue


The Notification Object

A notification is represented by an instance of the NSNotification class. A notification object contains several bits of state: a unique name, the posting object, and (optionally) a dictionary of supplementary information, called the userInfo dictionary. When a notification is delivered to an interested observer, the notification object is passed in as an argument of the method handling the notification.

Observing a Notification

To observe a notification, obtain the singleton NSNotificationCenter instance and send it an addObserver:selector:name:object: message. Typically, this registration step is done shortly after your application launches. The second parameter of the addObserver:selector:name:object: method is a selector that identifies the method that you implement to handle the notification. The method must have the following signature:

- (void)myNotificationHandler:(NSNotification *)notif;


In this handling method, you can extract information from the notification to help you in your response, especially data in the userInfo dictionary (if one exists).

Posting a Notification

Before posting a notification, you should define a unique global string constant as the name of your notification. A convention is to use a two- or three-letter application-specific prefix for the name, for example:

NSString *AMMyNotification = @"AMMyNotication";


To post the notification, send a postNotificationName:object:userInfo: (or similar) message to the singleton NSNotificationCenter object. This method creates the notification object before it sends the notification to the notification center.
 

4. Table View Editing - Table View 는 기본적으로 편집이 가능하다. 편집 버튼은 많은 기능을 제공하는데, 예를 들어, table view 로부터 행들을 삭제할 수 있도록 허용한다.

'Programming > iPhone, Xcode' 카테고리의 다른 글

Persistent Obj Store  (0) 2012.05.02
NSFetchedRequest  (0) 2012.05.02
Managed Object Context  (0) 2012.04.24
The Data Model  (0) 2012.04.12
iPad HIG, Universal App, Device Checking, Split View Controller  (0) 2012.03.23