如何制作一个带有按钮的UIPopover?

19

enter image description here

我想知道如何创建这样的带按钮的弹出框。

答案:

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...
    }
}
2个回答

45

这是一个UIActionSheet。在iPhone上,它从底部动画弹出。在iPad上,它以气泡形式出现。

假设您是通过按下按钮来执行此操作:

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
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];

或者在Swift中

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)

我只需要将这个添加到 Popover 视图中吗? - ManOx
不,只需使用其中一个UIActionSheet的showFrom...方法即可。请参见我的更新答案中的示例。 - Ashley Mills
好的,还有一个问题,我如何为按钮设置事件处理程序? - ManOx
我能否将新的Facebook分享按钮放在UIAlertController中,还是需要编写自定义弹出视图?我尝试了第二种方法,因为Storyboard似乎无法处理与特定UITableViewCell相关联的弹出视图转换,并且无法编译。 - PhillipOReilly

2

与其他回答类似,但相比之下这很容易实现。

让你的类使用 UIActionSheetDelegate

例如:

@interface ExampleViewController : UIViewController <UIActionSheetDelegate>

然后在你的ExampleViewController.mm/m中添加以下内容:
- (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)"); } }

现在,在按下按钮事件或需要弹出对话框的位置/时间中,调用以下内容:

    - (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 *)sender frame] inView:self.view animated:YES]; 来将弹出窗口附加到发送者按钮。 - Matt Privman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接