【问题标题】:UIAlertController error 'bounds' not found on object of type '__strong id'在“__strong id”类型的对象上找不到 UIAlertController 错误“边界”
【发布时间】:2015-07-06 19:40:05
【问题描述】:

我正在尝试创建一个提醒,当它出现时会询问用户是否要从他们的照片库中选择一张照片或拍照。我正在使用UIPopover How do I make a popover with buttons like this? 帖子中的模板。模板是...

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

但是在popover.sourceRect = sender.bounds; xcode 给我一个错误,指出Property 'bounds' not found on object of type '__strong id'。这个错误是什么意思?如何解决?

【问题讨论】:

    标签: ios objective-c xcode uialertcontroller


    【解决方案1】:

    您很可能接收到作为该函数的参数的 sender,其类型为 id,代表通用对象。编译器不知道它有一个名为bounds 的属性,因为它实际上可以是任何对象。要解决此问题,您需要通过强制转换来告诉它 sender 实际上是 UIView *

    UIView* senderView = (UIView *)sender; 
    

    然后你可以执行下一个任务:

    popover.sourceView = senderView;
    popover.sourceRect = senderView.bounds;
    

    【讨论】:

    【解决方案2】:

    问题是当您配置弹出框时。

    在警报控制器开始呈现之前,UIKit 框架不会创建 popoverPresentationController 属性。直到下一个显示更新周期才会真正显示它,所以不用担心它会先闪烁未配置。

    在显示警报控制器后进行配置,您应该没问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多