본문 바로가기

C/Objective C/ios

아이폰 DatePicker 만들기 예제 따라하기

- 아이폰의 날짜 및 시간을 설정할 수 있는  DatePicker을 이용한 간단한 예제이다.

- 화면 디자인

- DatePicker 만들기 순서

1. Xcode 실행

2. Single View Application

3. Project Name : “DatePickerTest”
4. storyboard에 Date Picker와 Label 위치
5. Assistant Editor로 Date Picker와 Label 드래그
6. “myDatePicker”, “myLabel” 입력
7. Date Picker을 Assistant Editor로 드래그 Connection을 Action으로 변경 “changeDatePicker” 입력 

8. ViewController.m 파일에 입력

-(IBAction)changeDatePicker {
//데이트 포맷터를 사용해 날짜 표시 형식을 정한다.
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; df.dateFromat = @“yyyy/MM/dd HH:mm”;
//지정한 날짜 형식으로 날짜를 표시한다.
myLabel.text = [df stringFromDate:myDatePicker.date];

}

9. 실행


- ViewController.m 소스코 드

#import "ViewController.h"


@interface ViewController (){

}


@end


@implementation ViewController

@synthesize myDatePicker;

@synthesize myLabel;


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}


- (void)viewDidUnload

{

    [self setMyDatePicker:nil];

    [self setMyLabel:nil];

    [super viewDidUnload];

    // Release any retained subviews of the main view.

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

} else {

    return YES;

}

}


- (IBAction)changeDatePicker:(id)sender {

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 

df.dateFormat = @"yyyy/MM/dd HH:mm";

//지정한 날짜 형식으로 날짜를 표시한다.

myLabel.text = [df stringFromDate:myDatePicker.date];

}

@end


- 실행화면