如何在 UIBarbutton 中将 UIAlertcontroller 实现为 PopOver(箭头向上) [英] How to implement UIAlertcontroller as PopOver(with arrow direction up) in a UIBarbutton

查看:22
本文介绍了如何在 UIBarbutton 中将 UIAlertcontroller 实现为 PopOver(箭头向上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是老生常谈的问题 - 但我确实在网上搜索过并找到了不推荐使用的解决方案.我将如何在 barButton 中将 UIAlertcontroller 实现为 popOver(箭头方向向上).代码如下:

I know that this is old school question - but I did searched the web and found solutions to be deprecated. How would I implement a UIAlertcontroller as popOver(with arrow direction up) in a barButton. Here's the code:

 - (IBAction)eventSortingAction:(UIBarButtonItem *)sender {

UIAlertController * view=   [UIAlertController
                         alertControllerWithTitle:@"My Title"
                         message:@"Select you Choice"
                         preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* ok = [UIAlertAction
                 actionWithTitle:@"OK"
                 style:UIAlertActionStyleDefault
                 handler:^(UIAlertAction * action) {
                     //Do some thing here
                     [view dismissViewControllerAnimated:YES completion:nil];

                 }];
UIAlertAction* cancel = [UIAlertAction
                     actionWithTitle:@"Cancel"
                     style:UIAlertActionStyleCancel
                     handler:^(UIAlertAction * action) {
                         [view dismissViewControllerAnimated:YES completion:nil];

                     }];
[view addAction:ok];
[view addAction:cancel];
[view setModalPresentationStyle:UIModalPresentationPopover];
view.modalInPopover = YES;
view.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
view.popoverPresentationController.delegate = self;
[self presentViewController:view animated:YES completion:nil];

UIView* senderView = [sender valueForKey:@"view"]; //HACK
UIPopoverPresentationController* popover = view.popoverPresentationController;
if (popover) {
   popover.sourceView = senderView;
   popover.sourceRect = senderView.bounds;
   popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
   popover.barButtonItem = self.actionBarButton;
   popover.delegate = self;
}}

显然我总是得到一个popover = nil".请帮忙!提前致谢!

apparently I always got a "popover = nil". Please Help! Thanks in advance!

顺便说一下,这段代码不是我的,只是在 Xcode 中测试它.

By the way this code is not mine, just testing it in Xcode.

推荐答案

提醒一下,因为这是 Google 上的最高搜索结果:

Just a reminded because this is a top result on Google :

popPresenter.barButtonItem 是 popPresenter.sourceView+popPresenter.sourceRect 的替代方案

popPresenter.barButtonItem is an alternative to popPresenter.sourceView+popPresenter.sourceRect

参见 (来源)

关于OP问题,应该使用IBAction参数sender.

Regarding OP question, IBAction parameter sender should be used.

UIPopoverPresentationController *popPresenter = [alertController
                                             popoverPresentationController];
popPresenter.barButtonItem = sender;
[self presentViewController:alertController animated:YES completion:nil];

这篇关于如何在 UIBarbutton 中将 UIAlertcontroller 实现为 PopOver(箭头向上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆