2012년 12월 22일 토요일

[iOS6] UIActionSheet 사용하기.


사용자에게 선택을 요청할 경우, UIActionSheet를 사용해서 여러 가지 선택을 할 수 있게 요청할 수 있다.

소스

#define STRING_TITLE @"I have a question."
#define STRING_CANCEL @"Cancel"
#define STRING_QUESTION1 @"What is your name?"
#define STRING_QUESTION2 @"Who you are?"
#define STRING_QUESTION3 @"I have no question."
#define STRING_QUESTION4 @"Question4"
#define STRING_QUESTION5 @"Question5"
#define STRING_QUESTION6 @"Question6"
#define STRING_QUESTION7 @"Question7"

- (IBAction)askQuestion:(id)sender
{
    UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:STRING_TITLE
                                                             delegate:self
                                                    cancelButtonTitle:STRING_CANCEL
                                               destructiveButtonTitle:STRING_QUESTION1
                                                    otherButtonTitles:STRING_QUESTION2, STRING_QUESTION3, STRING_QUESTION4, STRING_QUESTION5, STRING_QUESTION6,STRING_QUESTION7, nil];
    
    [actionSheet showInView:self.view];
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *choice = [actionSheet buttonTitleAtIndex:buttonIndex];
    if(buttonIndex == [actionSheet destructiveButtonIndex]) {
        //?
    }
    if([choice isEqualToString:STRING_QUESTION1]){
        //
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Question"
                                                        message:STRING_QUESTION1
                                                       delegate:self
                                              cancelButtonTitle:STRING_CANCEL
                                              otherButtonTitles: nil];
        [alert show];
    }else if([choice isEqualToString:STRING_QUESTION2]){
        //
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Question"
                                                        message:STRING_QUESTION2
                                                       delegate:self
                                              cancelButtonTitle:STRING_CANCEL
                                              otherButtonTitles: nil];
        [alert show];
    }else if([choice isEqualToString:STRING_CANCEL]){
        //Do Nothing..
    }
}
-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"actionSheet: didDismissWithButtonIndex:%d", buttonIndex);
}
-(void) actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"actionSheet: willDismissWithButtonIndex:%d", buttonIndex);
}


설명
아래 그림과 같이 5개 이하는 왼쪽 처럼 표시가 되고, 5개 이상이면, 오른쪽 처럼 표시가 된다.