1. Project 생성
  1) Create a new Xcode Project -> View based Application -> "AlertManager"

2. Action Button 연결하기
  1) Round Button 추가 -> 버튼명 설정
  2) header 파일에 함수 선언  : - (IBAction) clickButton;
  3) source 파일(*.m) 파일에 정의  : - (IBAction) clickButton;
  4) IB에서 Round Button 클릭 후 File's Owner로 오른쪽 마우스 클릭 후, Drag & Drop -> 함수연결

3.UIAlertView / UIActionSheet 사용하기
    -> 프로그램 코드 참고

4. UIAlertView / UIActionSheet 의 버튼 클릭 이벤트 사용하기
  1) Header 파일에 이벤트를 컨트롤 할 수 있는 Delegate Class를 상속받을 수 있도록 함 :
     ㄱ. UIAlertViewDelegate, UIActionSheetDelegate ; 
  2)  버튼 클릭시 호출되는 함수를 구현
     ㄱ. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
     ㄴ. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

   
5. 구현소스
...
@interface AlertManagerViewController : UIViewController <UIAlertViewDelegate, UIActionSheetDelegate> {
}

- (IBAction) clickSimple;
- (IBAction) clickMulti;
- (IBAction) clickActionSheet;

...

..
- (IBAction) clickSimple
{
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"질문에 답해주세요."  
message:@"아이폰 프로그래밍 너무 재미있죠?"  delegate:self 
cancelButtonTitle:@"뭐,그런걸 다" otherButtonTitles:@"당근이죠",nil];
 
[alert show];
[alert release]; 
}

- (IBAction) clickMulti
{
 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"솔직하게 답해주세요"
message:@"계속 아이폰 프로그래밍 공부 하실 건가요?" delegate:self
cancelButtonTitle:@"말할수 없음" otherButtonTitles:@"당근빳따",
@"기회가된다면!",@"싫어욧!",nil];
 
[alert show];
[alert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(alertView.title);
NSLog(@"Pressed button index : %ith", buttonIndex);
 
if(buttonIndex ==2 || buttonIndex ==3)
{
[self clickMulti];
}
}

- (IBAction) clickActionSheet
{
 UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"당신의 열정을 보여주세요" delegate:self cancelButtonTitle:@"뭐 이런걸 다 ..." destructiveButtonTitle:@"나의 열정은 마르지 않는 샘물" otherButtonTitles:@"안개속에서 나는 울었어~",@"외로워서 한참을 울었어~", nil];
 
[actionSheet showInView:[self view]];
[actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(actionSheet.title);
NSLog(@"Pressed button index : %ith", buttonIndex);
 
if(buttonIndex ==2 || buttonIndex ==3)
{
[self clickActionSheet];
}
}
..

6. 실행화면  
  1) clickSimple 실행화면

  2) clickMulti 실행화면

  3) clickActionSheet 실행화면


[특이사항]
1. UIAlertView의 버튼이 2개까지는 옆으로 정렬되어 나오고, 3개 이상부터는 수직으로 펼쳐진다.
2. 버튼 클릭시 발생하는 clickedButtonAtIndex 함수에서 인자로 넘어오는 buttonIndex 값이 UIAlertView와 UIActionSheet의 기준이 틀림
   1) UIAlertView : Canel 버튼이 항상 0이고, 나머지 버튼들이 순차적인 index를 지님
   2) UIActionSheet : Canel 버튼을 포함하여, 위에서부터 순차적인 index를 지님
3. 여러개의 Alert이나 ActionSheet가 있을 경우, title값을 확인하여 어쩐 질문인지를 파악할 수 있음.
   ( 스트링 비교가 부담될 것 같은데, 더좋은 방법이 있을까? : 물론 생성하는 alertView의 포인터를 관리하는 방법이 있겠으나 일반적으로 사용하는 방법은? )
 

Posted by 꿈을펼쳐라
,