Sharesheet from a tableViewCell popover on the ipad

Multi tool use
Multi tool use


Sharesheet from a tableViewCell popover on the ipad



I'm trying to share something to the sharesheet from a button inside a cusom cell on a UITableView thats presented as a popover. I keep getting:



Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modal view controller on itself. Presenting controller is .


func passToShareSheet(fileName: String, ext: String, stringToWriteToFile: String){
let fileName = "(fileName).(ext)"
let path = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
do {
try stringToWriteToFile.write(to: path!, atomically: true, encoding: String.Encoding.utf8)
let vc = UIActivityViewController(activityItems: [path as Any], applicationActivities: )
vc.popoverPresentationController?.sourceView = self
vc.present(vc, animated: true, completion: nil)
print(stringToWriteToFile)
} catch {
print("Failed to create file")
print("(error)")
}
}



I realize its because this is being called on a UITableViewCell but I have no idea how to do it the correct way? any help?





You need to change vc.present to theTableViewController.present. But of course you don't have a reference to that view controller from the cell.
– rmaddy
Jul 2 at 0:57


vc.present


theTableViewController.present





How do I get a reference to that from the cell?
– elmo5676
Jul 2 at 1:01





May be you can set a weak reference of the tableVIewController to the UItableViewCell when you are loading the UItableVIewCell. Thus you can avoid retain cycle and at the same time can also present the view controller.
– Karthick Ramesh
Jul 2 at 1:02





This code should be executed in your view controller, not in the cell. Use delegation or a closure to have the cell call code in the view controller
– Paulw11
Jul 2 at 1:04





Scan these search results
– rmaddy
Jul 2 at 1:06




1 Answer
1



Try below code



using button action of custom cell.


@IBAction func tapOnShowShareSheet(_ sender:UIButton) {
let buttonposition = sender.convert(CGPoint.zero, to: tableview_name)
if let indexpath = tableview_name.indexPathForRow(at: buttonposition) {

let stringToWriteToFile = stringToWriteToFile_using_indexpath
let fileName = fileName_from_cell_using_indexpath
let ext = ext_from_cell_using_indexpath

let fileName = "(fileName).(ext)"
let path = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
do {
try stringToWriteToFile.write(to: path!, atomically: true, encoding: String.Encoding.utf8)
let vc = UIActivityViewController(activityItems: [path as Any], applicationActivities: )
vc.modalPresentationStyle = .popover
vc.popoverPresentationController?.sourceView = self
vc.popoverPresentationController?.sourceView = sender
vc.popoverPresentationController?.sourceRect = sender.bounds
self.present(vc, animated: true, completion: nil)
print(stringToWriteToFile)
} catch {
print("Failed to create file")
print("(error)")
}
}
}





This works only if self is a view controller, which might not be the case
– Crazyrems
Jul 2 at 14:08


self





@Crazyrems actually the popoverPresentationController Will present on ViewController but the permittedArrowDirections will be on Sender Button of Cell.
– MAhipal Singh
Jul 2 at 14:29






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

AARo ID1bp m895fqvseikzox,kGmsA a8XNUP jZl5HfUW X5vqiKuRIe,p2UXfkIg0yY5 s hnHZl351C8RPa
y9tZUS0oWRldL95EgE89lQXOqvgmQSXeu1kqd5WWU pSQ8xuAlH

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications