본문 바로가기

C/Objective C/ios

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

-  Xcode 를 이용해서 아이폰 AlertView 를 만들어보자. 안드로이드에서  Dialog 과 비슷한 기능을 한다.

- AlertView를 이용해서 아이폰 어플리케이션 이용자에게 알림 서비스를 할 수 있을 것이다.

- 참고자료 http://www.edumobile.org/iphone/iphone-apps/showing-an-alert-in-iphone/

 

- 화면 디자인


-  UIAlertView 만들기 순서

1. Xcode 실행
2. Single View Application
3. Project Name : “UIAlertViewTest”
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{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Alert Test”

message:@“OK Cancel Test” delegate:self

cancelButtonTitle:@“Cancel” otherButtonTitles:@“OK”, nil]; [alert show];

}
-(void)alertView : (UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex {

if(buttonIndex == 1) { myLabel.text = @“OK”;

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

} }

10. Command+R


- 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 {

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert Test" message:@"OK Cancel Test" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

[alert show];

}


-(void)alertView : (UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex {

if(buttonIndex == 1) { 

myLabel.text =@"OK";

} else {

myLabel.text =@"Cancel";

}

@end


-실행화면