首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UIPopover如何使用这样的按钮制作弹出窗口?

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

Stack Overflow用户
提问于 2012-05-25 03:45:25
回答 1查看 15.5K关注 0票数 19

我想知道怎样才能用这样的按钮创建一个弹出窗口。

答案:

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...
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-25 03:56:13

这是一个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)
票数 45
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10744260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档