본문 바로가기

C/Objective C/ios

아이폰 Switch 만들기 예제 따라하기.

- Switch는 아이폰 어플리케이션 기능 중에서 설정 기능에 자주 사용 하는 기능으로써 간단하 예제로 배워보자.

- 참고자료 http://www.edumobile.org/iphone/miscellaneous/how-to-toggle-switch-in-iphone/

- 화면 디자인


- Switch 만들기 순서

1. Xcode 실행
2. Single View Application
3. Project Name : “SwitchTest”
4. storyboard에 Switch와 Label 위치
5. Assistant Editor로 Switch와 Label 드래그
6. “mySwitch”과 “myLabel” 입력
7. Button을 Assistant Editor로 드래그 Connection을 Action으로 변경 “changeSwitch“ 입력 

8. Control 키를 누를 상태에서 Assistant Editor에 있는 -(IBAction)changeSwitch; 에 드래그
9. ViewController.m 파일에 입력

-(IBAction)changeSwitch{ if(mySwitch.on == YES) {

myLabel.text = @“YES”; } else {

myLabel.text = @“NO”; }

}

10. Command+R


- ViewController.m 소스코드

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController

@synthesize myLabel;

@synthesize mySwitch;


- (void)viewDidLoad

{

    [super viewDidLoad];

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

}


- (void)viewDidUnload

{

    [self setMyLabel:nil];

    [self setMySwitch: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)changeSwitch:(id)sender {

if(mySwitch.on == YES)

myLabel.text = @"YES";

else 

myLabel.text = @"NO";

}

@end


- 실행화면