[英]UIActivityViewController - No share options - Swift 5
I have implemented a UIActivityViewController in Swift 5 via a nav bar button and upon click this should share the contents of an array called sharedData.我已经通过导航栏按钮在 Swift 5 中实现了一个 UIActivityViewController,单击它应该共享一个名为 sharedData 的数组的内容。
When clicking the button, the share options popup is empty.单击按钮时,共享选项弹出窗口为空。
Here is my func code:这是我的函数代码:
@IBAction func shareNavButton(_ sender: UIBarButtonItem) {
let shareItems: [Any] = [sharedData]
let activityVC = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityVC.popoverPresentationController?.barButtonItem = sender
self.present(activityVC, animated: true, completion: nil)
}
Here is my entire VC code:这是我的整个 VC 代码:
class FavoritesViewController: UIViewController {
@IBOutlet var tableView: UITableView!
@IBAction func shareNavButton(_ sender: UIBarButtonItem) {
let shareItems: [Any] = [sharedData]
let activityVC = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityVC.popoverPresentationController?.barButtonItem = sender
self.present(activityVC, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
convertArray()
}
}
extension FavoritesViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sharedData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.numberOfLines = 0
cell.textLabel?.text = sharedData[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath)
-> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Remove") { (_, _, completionHandler) in
sharedData.remove(at: indexPath.row)
saveArray()
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
completionHandler(true)
}
if #available(iOS 13.0, *) {
deleteAction.image = UIImage(systemName: "trash")
} else {
// Fallback to default action
}
deleteAction.backgroundColor = .systemRed
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
return configuration
}
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let copyAction = UIContextualAction(style: .normal, title: "Copy") { (_, _, completionHandler) in
let cell = tableView.cellForRow(at: indexPath)
UIPasteboard.general.string = cell?.textLabel?.text
completionHandler(true)
}
if #available(iOS 13.0, *) {
copyAction.image = UIImage(systemName: "doc.on.clipboard")
} else {
// fall back to default action
}
copyAction.backgroundColor = .systemBlue
let configuration = UISwipeActionsConfiguration(actions: [copyAction])
return configuration
}
}
You are using an array of string inside another array.您正在另一个数组中使用一个字符串数组。 Your sharedData is an array.
您的 sharedData 是一个数组。
let shareItems: [Any] = [sharedData]
Try with this code:试试这个代码:
let shareItems: [Any] = sharedData
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.