Warning when presenting UIActivityViewController

牧云@^-^@ 提交于 2019-12-11 02:59:30

问题


When i present an UIActivityController using the code below i get, it is presented but the console shows "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)
}

This func is called by an UILongPressGestureRecognizer. Note that i'm using storyboard with the following hierarchy:

TabBarController > (Relationship) > NavigationController > (Relationship) > TableViewController > (Show) > TableViewController > (Show) > ViewController.

The presentation happens on the last ViewController.

I'm quite sure it's about the hierarchy, which controller is currently presenting (and maybe how) and which controller is responsible for presenting the UIActivityViewController.

EDIT

UILongPressGestureRecognizer touch event is called multiple times which was causing the warning


回答1:


It's hard to say from your question but is there some other view controller presented at the moment this happens? for example and action sheet or other?

In any case try this:

    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)
    }



回答2:


I get same error message, because the UILongPressGestureRecognizer call many times my selector. Now I call UIActivityViewController when UILongPressGestureRecognizer state is end.

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

    //open UIActivityViewController
}


来源:https://stackoverflow.com/questions/30934881/warning-when-presenting-uiactivityviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!