【问题标题】:How to implement UIAlertcontroller as PopOver(with arrow direction up) in a UIBarbutton如何在 UIBarbutton 中将 UIAlertcontroller 实现为 PopOver(箭头方向向上)
【发布时间】:2016-04-25 01:26:05
【问题描述】:

我知道这是老派问题 - 但我确实搜索了网络并找到了要弃用的解决方案。我如何在 barButton 中将 UIAlertcontroller 实现为 popOver(箭头向上)。代码如下:

 - (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”。请帮忙!提前致谢!

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

【问题讨论】:

  • 使用实际创建UIAlertController 的代码更新您的问题。并解释您发布的代码实际发生的情况与您希望它执行的操作。
  • 嗨,rmaddy,我在下面发布了更新的问题。我无法编辑我的问题 - 仍然缺乏声誉。
  • 当然可以编辑。单击您的问题下的编辑链接。不要发布答案。
  • 谢谢没看到,那里更新了。所以我真正想要的是有一个警报消息,它用我拥有的 uibarbutton 锚定(有一个指向 uibarbutton 的箭头)。实际上发生的是它显示了传统的警报控件

标签: ios uibarbuttonitem uipopovercontroller uialertcontroller


【解决方案1】:

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

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

See(source)

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

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

【讨论】:

    【解决方案2】:
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *actnCamera = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * action) {
                                                       }];
    
    UIAlertAction *actnLibrary = [UIAlertAction actionWithTitle:@"Library" style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * action) {
                                                        }];
    
    [alertController addAction:actnLibrary];
    [alertController addAction:actnCamera];
    [alertController setModalPresentationStyle:UIModalPresentationPopover];
    UIPopoverPresentationController *popPresenter = [alertController
                                                     popoverPresentationController];
    popPresenter.sourceView = self.view;
    CGRect frame = self.navigationController.navigationBar.frame;
    frame.origin.x = self.navigationItem.leftBarButtonItem.width;
    popPresenter.sourceRect = frame;
    popPresenter.barButtonItem = self.navigationItem.leftBarButtonItem;
    [self presentViewController:alertController animated:YES completion:nil];
    

    输出

    【讨论】:

    • 嗨 PKT 它不起作用,它只是在我单击 uibar 按钮时显示警报控制器。我想要做的是,当我点击 uibarbutton 时就像这样 stackoverflow.com/questions/25805608/…
    • 我在 iPhone 中进行模拟,但仍然无法实现。这只能在 iPad 上完成吗?或者我只是错误地实施了这一点。我已经复制并粘贴了您的代码。
    • 为 iphone 创建包含 tableview 的自定义类并在您的通话中显示它,因为 popover 可能会解决您的问题 ....
    • 不要只发布代码。说明问题所在并说明您的回答如何解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 2020-08-19
    相关资源
    最近更新 更多