Search code examples
iosswiftipaduipopovercontrollerdidselectrowatindexpath

popoverPresentationController's sourceView inside didSelectRowAtIndexPath in iPad in Swift


I want to show an popoverPresentationController when user click an cell in iPad. Here's my code:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if(indexPath.section == 0){
        if(indexPath.row == 0){
            let textToShare = NSLocalizedString("SHARE_MESSAGE", comment: "Message...")

            if let appStoreURL = NSURL(string: "http://www.google.com/") {
                let objectsToShare = [textToShare, appStoreURL]
                let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

                self.presentViewController(activityVC, animated: true, completion: nil)

                if let popView = activityVC.popoverPresentationController {
                    let v = tableView as UIView
                    popView.sourceView = v
                    popView.sourceRect = v.bounds
                }
            }
        }
    }
}

It's wrong on iPad (nothing displayed on screen). So how can I solve it?

PS: Here's code for a UIButton and it's works fine on iPad (shown on screen):

@IBAction func shareButtonAction(sender: AnyObject) {
    let textToShare = NSLocalizedString("SHARE_MESSAGE", comment: "Message...")

    if let appStoreURL = NSURL(string: "http://www.google.com/") {
        let objectsToShare = [textToShare, appStoreURL]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

        self.presentViewController(activityVC, animated: true, completion: nil)

        if let popView = activityVC.popoverPresentationController {
            let v = sender as! UIView
            popView.sourceView = v
            popView.sourceRect = v.bounds
        }
    }
}

Solution

  • You are displaying the popover from the frame of the whole tableView so I suppose it's out of the screen. Try displaying it from the frame of the cell.

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if (indexPath.section == 0){
            if(indexPath.row == 0){
                let textToShare = NSLocalizedString("SHARE_MESSAGE", comment: "Message...")
    
                if let appStoreURL = NSURL(string: "http://www.google.com/") {
                    let objectsToShare = [textToShare, appStoreURL]
                    let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
    
                    self.presentViewController(activityVC, animated: true, completion: nil)
    
                    if let popView = activityVC.popoverPresentationController {
                        popView.sourceView = tableView
                        popView.sourceRect = tableView.cellForRowAtIndexPath(indexPath)!.frame
                    }
                }
            }
        }
    }