39

我正在使用 Xcode 6 开发 iOS 应用程序。

当我使用UIAlertController时,它可以在 iPhone 6 模拟器上运行良好,但在 iPad 模拟器上崩溃。

单击“共享”时出现我的问题,然后它可能会崩溃。我该如何解决?

这是我的代码:

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject] {

    var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Share", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

        let shareMenu = UIAlertController(title: nil, message: "Share using", preferredStyle: .ActionSheet)
        let twitterAction = UIAlertAction(title: "Twitter", style: UIAlertActionStyle.Default, handler: nil)
        let facebookAction = UIAlertAction(title: "Facebook", style: UIAlertActionStyle.Default, handler: nil)
        let emailAction = UIAlertAction(title: "Email", style: UIAlertActionStyle.Default, handler: nil)
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

        shareMenu.addAction(twitterAction)
        shareMenu.addAction(facebookAction)
        shareMenu.addAction(emailAction)
        shareMenu.addAction(cancelAction)

        self.presentViewController(shareMenu, animated: true, completion: nil)
        }
    )

Xcode 显示此消息:

******Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0xaf71c80>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem.  If this information is not known when you[![enter image description here][1]][1] present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0023c746 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x01c7aa97 objc_exception_throw + 44
    2   UIKit                               0x012c4062 -[UIPopoverPresentationController presentationTransitionWillBegin] + 3086
    3   UIKit                               0x00bda174 __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke + 1549
    4   UIKit                               0x00bd8247 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 198
    5   UIKit                               0x00c0d31b __40+[UIViewController _scheduleTransition:]_block_invoke + 18
    6   UIKit                               0x00ac6862 ___afterCACommitHandler_block_invoke + 15
    7   UIKit                               0x00ac680d _applyBlockToCFArrayCopiedToStack + 415
    8   UIKit                               0x00ac6622 _afterCACommitHandler + 549
    9   CoreFoundation                      0x0015d86e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    10  CoreFoundation                      0x0015d7b0 __CFRunLoopDoObservers + 400
    11  CoreFoundation                      0x001531ea __CFRunLoopRun + 1226
    12  CoreFoundation                      0x00152a5b CFRunLoopRunSpecific + 443
    13  CoreFoundation                      0x0015288b CFRunLoopRunInMode + 123
    14  GraphicsServices                    0x047b82c9 GSEventRunModal + 192
    15  GraphicsServices                    0x047b8106 GSEventRun + 104
    16  UIKit                               0x00a9c106 UIApplicationMain + 1526
    17  Mars I                              0x0001c724 main + 180
    18  libdyld.dylib                       0x02392ac9 start + 1
    19  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException******
4

11 回答 11

56

为了保持它独立于设备,不如使用它,如下面的代码片段所示。此代码将为 iPhone / 紧凑型设备上的 popoverPresentationController 返回 nil,因此您可以在通用项目中安全地使用它。

if let popoverPresentationController = shareMenu.popoverPresentationController {
    popoverPresentationController.sourceView = self.view
    popoverPresentationController.sourceRect = sender.bounds
}
self.presentViewController(shareMenu, animated: true, completion: nil)
于 2016-01-13T08:59:45.760 回答
40

试试这个代码:

shareMenu.popoverPresentationController.sourceView = self.view
shareMenu.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0, 1.0, 1.0)

self.presentViewController(shareMenu, animated: true, completion: nil)
于 2015-07-23T03:04:25.300 回答
12

斯威夫特 4

popoverPresentationController.permittedArrowDirections = .init(rawValue: 0)
popoverPresentationController.sourceView = self.view
popoverPresentationController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
于 2017-11-16T08:40:17.920 回答
11

参考: Swift 中 iPad 上的 ActionSheet Popover

创建 popoverController :

let alertController = UIAlertController(title: nil, message: "Alert message.", preferredStyle: .actionSheet)
self.present(alertController, animated: true, completion: nil)

如果你想用指示器视图显示警报应该:

if let popoverController = alertController.popoverPresentationController {
    popoverController.barButtonItem = sender
}

带指示灯

在中心显示警报:

if let popoverController = alertController.popoverPresentationController {
  popoverController.sourceView = self.view
  popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) 
}

中心带指示器

中心无指标:

if let popoverController = alertController.popoverPresentationController {
  popoverController.sourceView = self.view
  popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
  popoverController.permittedArrowDirections = []
}

中心无指示

于 2018-11-05T07:55:55.917 回答
7

我也面临同样的问题,终于得到了解决方案。这是我对 Objective-C 的解决方案:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Select you option:"
                                                                         message:nil
                                                                  preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *action = [UIAlertAction actionWithTitle:@“share”
                                                     style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction *action) {
                                                       // do other things
                                                   }];
[alertController addAction:action];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                       style:UIAlertActionStyleCancel
                                                     handler:^(UIAlertAction *action) {
                                                     }];
[alertController addAction:cancelAction];

// Remove arrow from action sheet.
[alertController.popoverPresentationController setPermittedArrowDirections:0];

//For set action sheet to middle of view.
CGRect rect = self.view.frame;
rect.origin.x = self.view.frame.size.width / 20;
rect.origin.y = self.view.frame.size.height / 20;
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.sourceRect = rect;

[self presentViewController:alertController animated:YES completion:nil];
于 2017-01-05T06:19:16.973 回答
2

我使用这个方便的扩展来创建永不崩溃的操作表

extension UIAlertController {

    class func actionSheetWith(title: String?, message: String?, sourceView: UIView?, sourceFrame: CGRect?) -> UIAlertController {
        let actionController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
        if actionController.responds(to: #selector(getter: popoverPresentationController)) {
            actionController.popoverPresentationController?.sourceView = sourceView ?? StoryboardHelper.tabBarControllerTopController()?.view
            actionController.popoverPresentationController?.sourceRect = sourceFrame ?? CGRect(x: 0, y: 0, width: 0, height: 0)
        }
        return actionController
    }

}
于 2017-05-31T20:51:00.270 回答
1

我使用此代码,非常简单易懂,也许我可以为任何人提供帮助

//if iPhone
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        [self presentViewController:actionSheet animated:YES completion:nil];
    }
    //if iPad
    else {
        // Change Rect to position Popover
        UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:actionSheet];
        [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
于 2017-12-08T12:49:31.643 回答
1

我发现这个链接很有用,对我有用

https://medium.com/@nickmeehan/actionsheet-popover-on-ipad-in-swift-5768dfa82094

进行了如下细微更改以调整对齐方式。

if let popoverPresentationController = alert.popoverPresentationController {
        popoverPresentationController.sourceView = self.view
        popoverPresentationController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.size.height * 0.85, width: 0, height: 0)
        popoverPresentationController.permittedArrowDirections = []
    }
于 2021-01-15T12:34:33.477 回答
0

如果您不需要将警报显示为弹出框,则可以UIAlertController.Style.alert在初始化时简单地使用作为首选样式AlertViewController

let shareMenu = UIAlertController(title: nil, message: "Share using", preferredStyle: .alert)
于 2020-01-03T21:37:28.407 回答
0

斯威夫特 3

shareMenu.popoverPresentationController?.sourceView = self.view
shareMenu.popoverPresentationController?.sourceRect = 
    CGRect(x: view.bounds.size.width, 
           y: view.bounds.size.height-80, 
           width: 1.0, height: 1.0)

source rect 是您要显示弹出视图的起点。

于 2017-08-16T09:04:41.120 回答
0

适用于iOS 13

用过。

 if #available(iOS 13.0, *) {
            if let popoverPresentationController = actionSheet.popoverPresentationController {
                popoverPresentationController.sourceView = self.view
                popoverPresentationController.sourceRect = sender.frame
            }
            self.present(actionSheet, animated: true, completion: nil)
        } else {
            actionSheet.popoverPresentationController?.sourceView = self.view
            actionSheet.popoverPresentationController?.sourceRect = sender.frame

            actionSheet.show()
        }
于 2019-10-16T11:31:36.960 回答