ios - UIPopover 如何使用这样的按钮制作弹出窗口?

标签 ios ipad uipopovercontroller uiactionsheet uialertcontroller

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

委托(delegate)对象类中的其他地方...

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

最佳答案

这是一个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 * 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)

关于ios - UIPopover 如何使用这样的按钮制作弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10744260/

相关文章:

javascript - 检测ios、android或桌面是否启动不同的链接?

iphone - 如何使拖动的 UIView 减速?

iOS modal view transition flip 无需整屏翻转

ios - 内部有 TableView 的弹出 Controller 的固定 subview

iphone - 从 self.navigationItem(在 UINavigationController 内部)打开时,UIPopoverController 未解除

ios - Swift 数组,文本文件?

ios - 获取 "Type ' Float' 不符合协议(protocol) 'ForwardIndex' “Xcode Beta 4 中的范围错误

ios - 如何缩放physicsBody?

iphone - 将 kABemailProperty 转换为 NSString

ios - Swift iOS - 如何将 UIPopoverBackgroundView 类中的方法连接到不同类中的 PopoverController?