본문 바로가기

C/Objective C/ios

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

- 아이폰 하단에서 올라오는 메뉴바라고 할 수 있는 ActionSheet를 만들어 보자. 안드로이드에서 메뉴키를 눌렀을때와 동일하다고 할 수 있다.

- 화면 디자인


- UIActionSheet 만들기 순서

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

 8. Control 키를 누를 상태에서 Assistant Editor에 있는 -(IBAction)tapBtn; 에 드래그 

9. ViewController.m 파일에 입력

-(IBAction)tapBtn{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@“Title”

delegate:self cancelButtonTitle:@“Cancel” destructiveButtonTitle:@“주의”

otherButtonTitles:@“선택A”, @“선택 B”, nil]; [actionSheet showInView:self.view];

}
-(void)actionSheet : (UIActionSheet *)actionSheet clickedButtonAtIndex:

(NSInteger)buttonIndex { myLabel.text = [NSString stringWithFormat : “버튼 = %i”, buttonIndex];

}


ViewController.m 소스코드

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController

@synthesize myLabel;

@synthesize myButton;


- (void)viewDidLoad

{

    [super viewDidLoad];

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

}


- (void)viewDidUnload

{

    [self setMyLabel:nil];

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

UIActionSheet * actionSheet = [[UIActionSheet alloc]initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"주의" otherButtonTitles:@"선택A",@"선택 B", nil];

[actionSheet showInView:self.view];


}

-(void)actionSheet : (UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

myLabel.text=[NSString stringWithFormat:@"버튼 =%i",buttonIndex];

}

@end


- 실행 화면