본문 바로가기

프로그래밍/아이폰 프로그래밍

1. Hello World! (UILabel, UIButton)

- File>New Project>View-based Application
- project name : HelloWorld
- 코드 Editor : View>Zoom Editor Out



1. 인터페이스 만들기

 HelloWorldViewControler.xib를 수정해야한다.
인터페이스 빌더를 실행 
View의 창이 아이폰 창의 영역과 같다.
View window에 library오브젝트로 꾸며준다.



2. 코드추가

HelloWorldViewController.h

#import <UIKit/UIKit.h>


@interface HelloWorldViewController : UIViewController {

IBOutlet UILabel *label_hello; // 인터페이스 빌더에서 배치한 요소들 (Outlet)

IBOutlet UIButton *button_iphone;

IBOutlet UIButton *button_ipad;

IBOutlet UIButton *button_ipodtouch;

}


- (IBAction) button1Touched; // 이벤트 등록 (Action)

- (IBAction) button2Touched;

- (IBAction) button3Touched;


@property (nonatomic, retain) IBOutlet UILabel *label_hello;    // getter/setter를 자동으로 생성하기

@property (nonatomic, retain) IBOutlet UIButton *button_iphone; // 위한 @property

@property (nonatomic, retain) IBOutlet UIButton *button_ipad;

@property (nonatomic, retain) IBOutlet UIButton *button_ipodtouch;


@end



HelloWorldViewController.m


#import "HelloWorldViewController.h"


@implementation HelloWorldViewController


@synthesize label_hello; // 헤더파일에서 프로퍼티를 선언한 것처럼 .m파일에서는 @synthesize 선언

@synthesize button_iphone;

@synthesize button_ipad;

@synthesize button_ipodtouch;


- (IBAction) button1Touched{ // 이벤트 동작을 구현한다.

self.label_hello.text = @"Hello iPhone";

}

- (IBAction) button2Touched{

self.label_hello.text = @"Hello iPad";

}

- (IBAction) button3Touched{

self.label_hello.text = @"Hello iPod Touch";

}



- (void)didReceiveMemoryWarning {

// Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.

}


- (void)viewDidUnload {

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}


// Outlet이 사용한 메모리를 해제해준다.

- (void)dealloc {

[label_hello release];

[button_iphone release];

[button_ipad release];

[button_ipodtouch release];

    [super dealloc];

}


@end





3. 인터페이스와 코드 연결하기


HelloWorldViewcontroller.xib 인터페이스 빌더 실행

Document Window>File's Owner 선택

Inspector>Connections 선택 

Outlet과 Action들을 실제 UI요소와 연결