18

enter image description hereUIPopover 이와 같은 버튼을 사용하여 팝업을 만드는 방법은 무엇입니까?

이렇게 단추가있는 팝업을 어떻게 만들 수 있는지 궁금합니다.

답변 : 위임 된 객체 클래스의 다른

UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil 
                  delegate: self 
               cancelButtonTitle: nil 
              destructiveButtonTitle: nil 
               otherButtonTitles: @"Take Photo", 
                    @"Choose Existing Photo", nil]; 

[actionSheet showFromRect: button.frame inView: button.superview animated: YES]; 

어딘가에 ...

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     // take photo... 
    } 
    else if (buttonIndex == 1) { 
     // choose existing photo... 
    } 
} 

답변

43

이것은 UIActionSheet이다. iPhone에서는 아래에서부터 움직입니다. iPad에서는 popover에 나타납니다.

당신이 버튼을 누르면에이 일을하고 가정 :

UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil 
                  delegate: self 
               cancelButtonTitle: nil 
              destructiveButtonTitle: nil 
               otherButtonTitles: @"Take Photo", 
                    @"Choose Existing Photo", nil]; 

[actionSheet showFromRect: button.frame inView: button.superview animated: YES]; 

iOS8의 +에서 새 UIAlertController 클래스를 사용해야합니다

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil 
                      message: nil 
                    preferredStyle: UIAlertControllerStyleActionSheet]; 
[alertController addAction: [UIAlertAction actionWithTitle: @"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    // Handle Take Photo here 
}]]; 
[alertController addAction: [UIAlertAction actionWithTitle: @"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    // Handle Choose Existing Photo here 
}]]; 

alertController.modalPresentationStyle = UIModalPresentationPopover; 

UIPopoverPresentationController * popover = alertController.popoverPresentationController; 
popover.permittedArrowDirections = UIPopoverArrowDirectionUp; 
popover.sourceView = sender; 
popover.sourceRect = sender.bounds; 

[self presentViewController: alertController animated: YES completion: nil]; 

또는 스위프트에서

let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet) 
alertController.addAction(UIAlertAction(title: "Take Photo", style: .Default, handler: { alertAction in 
    // Handle Take Photo here 
    })) 
alertController.addAction(UIAlertAction(title: "Choose Existing Photo", style: .Default, handler: { alertAction in 
    // Handle Choose Existing Photo 
    })) 
alertController.modalPresentationStyle = .Popover 

let popover = alertController.popoverPresentationController! 
popover.permittedArrowDirections = .Up 
popover.sourceView = sender 
popover.sourceRect = sender.bounds 

presentViewController(alertController, animated: true, completion: nil) 
+0

그냥 Popover보기에 추가합니까? – ManOx

+0

아니요. UIActionSheet showFrom ... 메서드 중 하나를 사용하십시오. 예를 들어 업데이트 된 답변보기 –

+0

좋아요. 질문 1 개만 추가하면 단추에 이벤트 처리기를 어떻게 설정합니까? – ManOx

2

다른 응답과 유사하지만 비교하기가 매우 쉽습니다.

클래스에 UIActionSheetDelegate를 사용하십시오.

예 :

@interface ExampleViewController : UIViewController <UIActionSheetDelegate> 

그런 다음 버튼을 누르면 이벤트에

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { //Get the name of the current pressed button 
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex]; 
if ([buttonTitle isEqualToString:@"Remove"]) { 
    NSLog(@"Remove this actionSheet"); } 
if ([buttonTitle isEqualToString:@"Button 1"]) { 
    NSLog(@"Button 1 pressed"); } 
if ([buttonTitle isEqualToString:@"Button 2"]) { 
    NSLog(@"Button 2 pressed"); } 
if ([buttonTitle isEqualToString:@"Button 3"]) { 
    NSLog(@"Button 3 pressed"); } 
if ([buttonTitle isEqualToString:@"Cancel"]) { 
    NSLog(@"Cancel clicked (anywhere away from it)"); } } 

이제 ExampleViewController.mm/m에 추가하거나 원하는/때 곳은 다음 전화를 팝업하기 :

- (IBAction)aButtonPressed:(id)sender { 
    NSString *actionSheetTitle = @"Action Sheet"; // Title 
    NSString *destroyTitle = @"Destroy"; // Button titles 
    NSString *button1 = @"Button 1"; 
    NSString *button2 = @"Button 2"; 
    NSString *button3 = @"Button 3"; 
    NSString *cancelTitle = @"Cancel"; 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                  initWithTitle:actionSheetTitle 
                                  delegate:self 
                                   cancelButtonTitle:cancelTitle 
                                  destructiveButtonTitle:destroyTitle 
                                  otherButtonTitles:button1, button2, button3, nil]; [actionSheet showInView:self.view]; 
} 

자세한 내용은 @ : http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html

+1

[actionSheet showFromRect : [(UIButton *) 보낸 사람 프레임] inView : self.view animated : YES];를 추가하는 것을 잊지 마십시오. popover를 발신자 버튼에 첨부합니다. –