Transcript iPhone_1_1

iPhone Seminar Plus
2010 . 04 . 27 . 화
정보통신연구소 유승구 연구원
Rev. No. 1.1
Last modified: 2010/04/27
2010-04-27
NOVAPEX Mobile Co., LTD
Confidential
Agenda
◈ iPhone : application develop
1. 응용프로그램 예제
1. 응용프로그램 템플릿 종류
2. Hello~ iPhone World 예제 소스 분석
3. 자격증요리 요약집 프로젝트 첫 화면 제작(컨트롤 아이템 사용 예)
2. 과제
2
Confidential
1-1. 응용프로그램 템플릿 종류
◈ Xcode 새 프로젝트 생성시 선택 가능한 템플릿
Application Project
계층적 Data를 다중 뷰를 이용해 표
시하는 응용프로그램 템플릿.
단일 뷰를 생성하여 유저 인터페이
스를 향상시키는 응용프로그램 템플
릿.
Ex_) 연락처 응용프로그램
이미지나 애니매이션을 OpenGL ES
기반의 뷰로 제작하여 뿌려주는 응
용프로그램 템플릿
delegate와 window를 포함한 응용
프로그램의 다양한 시작점을 제시하
는 템플릿. 사용자의 뷰 계층을 향상
시키고 싶을 때 사용
라디오 인터페이스의 응용프로그램
템플릿. 사용자가 선택한 뷰를 보여
주게 된다.
메인 뷰와 flipside 뷰를 간단하게 수
정하여 개발 할 수 있는 응용프로그
램 템플릿.
EX_) 주식 응용프로그램
3
Confidential
1-1. 응용프로그램 템플릿 종류
◈ Xcode 새 프로젝트 생성시 선택 가능한 템플릿
Navigation-based Application (메일)
4
Confidential
1-1. 응용프로그램 템플릿 종류
◈ Xcode 새 프로젝트 생성시 선택 가능한 템플릿
Tab bar application
5
Confidential
1-1. 응용프로그램 템플릿 종류
◈ Xcode 새 프로젝트 생성시 선택 가능한 템플릿
Single view based application
6
Confidential
1-2. Hello~ iPhone World 예제
◈ 실행 화면
사용 컨트롤
- Label
- Round Rect Button
목표 : Button에 함수를 연결하여 이벤트에 따른 동작 수행
Outlet에 대한 이해와 이를 연동 하는 방법 이해
소스 작업 후 Interface Builder 작업 진행
7
Confidential
1-2. Hello~ iPhone World 예제
◈ 애플리케이션의 MVC 구조
Delegate 위임자
UIApplication의 위임자로
UIApplication에 해당되는 이벤트를
처리하여 View(UIWindow)로 반영
한다.
8
Confidential
1-2. Hello~ iPhone World 예제 소스
◈ 소스 분석
HelloWorldAppDelegate.h, HelloWorldAppDelegate.m 파일 수정
9
Confidential
1-2. Hello~ iPhone World 예제 소스
◈ 소스 분석 main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil); // 응용프로그램 객체, delegate 생성, event cycle 설정
Interface Builder 파일을 로드 함
[pool release];
응용프로그램 종료시 exit(0)함수를 호출하며 이를 반환함
return retVal;
}
<retain count>
메모리 관리를 위해 객체가 생성될 때마다 그 수를 증가 시키고 릴리즈 시마다 수를 감소 시킨다.
count의 값이 0이 되면 더 이상 사용이 불가능 하다.
<NSAutoreleasePool>
autorelease 메서드를 통해 소유권이 해지된 객체들을 가진 일종의 객체 풀(공간)
autorelease pool에 있는 객체들은 해당 Autorelease Pool이 메모리에서 폐기되는 순간에 release 메서드가 호출됨
Java나 C#의 가비지 컬렉터와 비슷한 개념으로 특이한 점은 Autorelease Pool은 특정 블록에 Pool을 지정하거나 Pool
에 들어갈 객체를 선택할 수 있는 점이 다르다.
10
Confidential
1-2. Hello~ iPhone World 예제 소스
◈ 애플리케이션 라이프 사이클
UIKit 프레임 워크에서 main() 메소드를 호출함으로써 시작
system asks application to terminate
application Will terminate
Application execution terminates
아직 적용되지 않은 프로토콜
11
Confidential
1-2. Hello~ iPhone World 예제 소스
◈ HelloWorldAppDelegate.h 파일 소스 분석
#import <UIKit/UIKit.h>
// UIApplicationDelgate protocol : 객체와의 통신을 위해 정해놓은 메소드 규약
@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
– applicationDidFinishLaunching:
IBOutlet UILabel *label; // 추가
– application:didFinishLaunchingWithOptions
}
– applicationWillTerminate:
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UILabel *label; // 추가
– applicationDidReceiveMemoryWarning:
– applicationSignificantTimeChange:
-(IBAction)PressTheButton:(id)sender;
@end
IBOutlet / IBAction : Interface Builder 파일과 연결되는 인스턴스 변수와 액션 메소드임을 선언하는 예약어
(id)sender : IBAction을 통해 연결된 액션 메소드의 파라미터로 호출한 UI 컨트롤 오브젝트가 넘겨진다.
어떤 타입의 컨트롤 오브젝트 인지 모르기 때문에 (id)를 앞에 써준다.
12
Confidential
1-2. Hello~ iPhone World 예제 소스
◈ HelloWorldAppDelegate.h 파일 소스 분석
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UILabel *label; // 추가
<property, synthesize 개념>
인스턴스 오브젝트의 변수를 외부에서 접근하기 위해서는 엑세서 메소드가 필요
모든 변수에 해당하는 엑세서 메소드를 만드는 것을 편의상 하기 위해 property로 지정하여 생성시켜줌
인스턴스 변수를 프라퍼티로 선언(헤더 파일)하면 implement 파일(소스 파일)에서 메소드를 구현할 필요가 없다.
단! 컴파일러에게 기본 엑세서 메소드를 만들어 달라고 요청해야 하므로 아래와 같이 implement파일에 작성한다.
@synthesize label
별도의 setter, getter 메소드를 작성하지 않아도 사용이 가능하다.
사용자 정의에 맞게 Setter나 Getter의 이름을 작성할 수 있다.
EX_) [label setText:@"Hi~"];
13
Confidential
1-2. Hello~ iPhone World 예제 소스
◈ HelloWorldAppDelegate.h 파일 소스 분석
<property 속성>
@property (<#attributes#>) <#type#> <#name#>;
<setter의 동작 방식 : 프라퍼티가 최종적으로 넘겨진 파라미터(매개변수)를 가지는 방식>
assign : 넘어온 값을 그대로 가진다. int, float, bool 같은 스칼라 값을 다루는 프로퍼티
retain : 넘어온 오브젝트의 리테인 카운트를 1증가시켜 보관하며 이전 값은 release 된다.
copy : 넘어온 매개변수를 복사해서 보관하며 이전 값은 release된다. (주의점 NSCopying 프로토콜을 준수해야함)
<nonatomic>
atomic : 쓰레드에 안전한 프라퍼티 생성 (기본값)
nonatomic : 쓰레드 안전 장치가 없는 프라퍼티 생성 (설정값)
<readwrite, readonly>
읽기/쓰기 속성을 설정, readonly의 경우 getter만 제공된다.
14
Confidential
1-2. Hello~ iPhone World 예제 소스
◈ HelloWorldAppDelegate.m 파일 입력
#import "HelloWorldAppDelegate.h"
@implementation HelloWorldAppDelegate
@synthesize window;
@synthesize label; // 추가 getter, setter를 추가해달라고 컴파일러에게 통보
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch
[window makeKeyAndVisible]; // 리시버 키 윈도우 생성 및 화면 레이어 제일 상단에 표시
return YES;
}
-(IBAction)PressTheButton:(id)sender{
label.text = @"Hello iPhone World"; // 아래의 표현식과 동일한 표현
[label setText:@"Hello iPhone World"]; // setter를 이용한 값 입력
}
- (void)dealloc {
[label release]; // label에 관한 메모리 릴리즈
[window release];
[super dealloc];
}
@end
15
Confidential
1-2. Hello~ iPhone World 예제 소스
◈ Interface Builder를 이용한 View 환경 구성
Resources 폴더에 있는 MainWindow.xib를 더블 클릭한다.
16
Confidential
1-2. Hello~ iPhone World 예제 소스
◈ Interface Builder를 이용한 View 환경 구성
드래그 & 드롭을 이용해 컨트롤러를 배치한다. (Label, Round Rect Button)
17
Confidential
1-2. Hello~ iPhone World 예제 소스
◈ Interface Builder를 이용한 View 환경 구성
버튼 선택 후 윈도우 시작 키 + 2 를 누르면 Button Connections 창이 활성화 된다.
Touch Up Inside 옆의 동그라미를 누르고 이동하면 선이 생성 되는데 Hello World app Delegate에 가져다 댄다.
네모 박스가 생성되고 마우스 클릭을 해제 하면 PressTheButton이 표시된다. 선택해준다.
18
Confidential
1-2. Hello~ iPhone World 예제 소스
◈ Interface Builder를 이용한 View 환경 구성
Hello World App Delegate를 선택하면 Connections 창이 활성화 된다.
앞서 등록한 Label Outlet에 Label control을 링크 시킨다.
19
Confidential
1-2. Hello~ iPhone World 예제 소스
◈ 컴파일 및 실행 결과
Build & Run 을 눌러 실행 시킨다.
(윈도우 시작키 + B : 컴파일)
(윈도우 시작키 + R : 컴파일 후 실행)
- 아이폰 시뮬레이터 구동 후 왼쪽과 같은 화면이 나온다.
- 버튼을 누르면 Hello iPhone World 가 나타나게 된다.
- 정상적으로 동작하지 않는다면 Interface Builder에서 설정을 다시 해본다
20
Confidential
1-3. 자격증요리 요약집 메인화면
◈ 메인화면 완성 이미지
사용 컨트롤
- Image View
- Round Rect Button 3개
- 관련 리소스 : 배경 이미지, 버튼 이미지
목표 : Button, Image view 컨트롤러의 속성에 대해 알고
Button에 함수를 연결하여 이벤트에 따른 동작 수행(실습)
21
Confidential
1-3. 자격증요리 요약집 메인화면
◈ Image View control
단일 이미지 보여주거나 여러 장의 이미지를 배열에 저장하여 애니매이션으로 표시하는 컨트롤
Image View
Image : 원하는 이미지를 선택하면 해당 이미지가 뿌려진다.
(프로젝트 리소스에 이미지 추가시 자동으로 선택 가능)
View(공통)
Mode : 이미지를 어떻게 배치 할 지 선택 할 수 있다.
Alpha : 투명도를 지정 할 수 있다.
Background : 배경색을 지정 할 수 있다. (기본 투명색)
Tag : Index와 같은 개념, 애니매이션의 그림 순서라고 생각하면 편하다.
Drawing : 체크 박스를 통해 원하는 상태로 그릴 수 있다.
Interaction : 사용자의 이벤트에 따라 반응하게 할지를 결정한다.
22
Confidential
1-3. 자격증요리 요약집 메인화면
◈ Round Rect Button Attributes
터치 이벤트를 입력받아 타겟이 되는 객체에게 액션 메시지를 전달하는 역할을 한다.
Button
type : 버튼의 형태를 결정한다.
(custom : 사용자 정의 형태, Round Rect :
)
(SDK에서 제공되는 버튼의 모양 형태 : Detail discloser , info light,
info Dark, Add Contact)
Title : 버튼에 적용되는 글자
Image : 버튼에 적용되는 이미지 (원본의 비율을 유지)
Background : 버튼에 적용되는 이미지의 배경(원본 비율 유지 안함)
Line Breaks : 텍스트가 라벨을 넘어갈 때 어떻게 처리되는지 설정
Edge Inset : 버튼 내의 내용 타이틀 이미지가 표시될 위치를 설정한다.
23
Confidential
1-3. 자격증요리 요약집 메인화면
◈ appMainAppDelegate.h 파일 입력
#import <UIKit/UIKit.h>
@class foodListTableController;
@interface appMainAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
-(IBAction)PressTheExit:(id)sender;
@end
24
Confidential
1-3. 자격증요리 요약집 메인화면
◈ appMainAppDelegate.m 파일 입력
#import "appMainAppDelegate.h"
@implementation appMainAppDelegate
@synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch
[window makeKeyAndVisible];
return YES;
}
-(IBAction)PressTheExit:(id)sender
{
exit(0);
}
- (void)dealloc {
[window release];
[super dealloc];
}
@end
25
Confidential
1-3. 자격증요리 요약집 메인화면
◈ 버튼을 액션 이벤트를 받는 객체(delegate된 App Main App Delegate)에 연결
26
Confidential
2. 과제
◈ 실습
1. 방금 진행한 메인화면 예제 실습 진행
2. 나머지 두 버튼에 대한 이벤트 함수를 추가하여 NSLog를 이용해 버튼이 눌렸을 때의 결과를
디버거를 통해 확인
(완성된 실습 코드는 포럼에서 받을 수 있습니다.)
링크 : http://115.92.203.235/drupal/?q=forum/12
27
Confidential
마무리
감사합니다.
28
Confidential