呈现 UIActivityViewController 时发出警告

Warning when presenting UIActivityViewController

当我使用下面的代码显示 UIActivityController 时,它显示了,但控制台显示“Warning: Attempt to present <UIActivityViewController: 0x7f8788e7aed0> on <MyApp.CustomTableViewController: 0x7f8788e3db60> which is already presenting (null)”。

@IBAction func shareImage(sender: AnyObject) {
    let images: [UIImage] = [image.image!]
    let activityViewController = UIActivityViewController(activityItems: images, applicationActivities: nil)
    self.presentViewController(activityViewController, animated: true, completion: nil)
}

此函数由 UILongPressGestureRecognizer 调用。请注意,我使用的故事板具有以下层次结构:

TabBarController>(关系)>NavigationController>(关系)>TableViewController>(显示)>TableViewController>(显示)>ViewController .

演示发生在最后一个 ViewController。

我很确定这是关于层次结构的,哪个控制器当前正在呈现(以及可能如何呈现)以及哪个控制器负责呈现 UIActivityViewController

编辑

UILongPressGestureRecognizer 多次调用触摸事件导致警告

很难从你的问题中得出结论,但在发生这种情况时是否还有其他视图控制器?例如和操作 sheet 或其他?

无论如何试试这个:

    if self.presentedViewController != nil {
        self.dismissViewControllerAnimated(false, completion: {
            [unowned self] in
            self.presentViewController(activityViewController, animated: true, completion: nil)
            })
    }else{
        self.presentViewController(activityViewController, animated: true, completion: nil)
    }

我收到相同的错误消息,因为 UILongPressGestureRecognizer 多次调用我的选择器。现在我在 UILongPressGestureRecognizer 状态结束时调用 UIActivityViewController。

@objc func longTouch(gestureRecognizer: UILongPressGestureRecognizer){
    print("long touch")
    if gestureRecognizer.state != .ended {
         print("long touch not ended")
        return
    }

    //open UIActivityViewController
}