UIActivityViewController - 没有共享选项 - Swift 5
UIActivityViewController - No share options - Swift 5
我在 Swift 5 中通过导航栏按钮实现了一个 UIActivityViewController,点击它应该会共享名为 sharedData 的数组的内容。
单击按钮时,共享选项弹出窗口为空。
这是我的函数代码:
@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)
}
这是我的全部 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
}
}
您正在另一个数组中使用一个字符串数组。您的 sharedData 是一个数组。
let shareItems: [Any] = [sharedData]
尝试使用此代码:
let shareItems: [Any] = sharedData
我在 Swift 5 中通过导航栏按钮实现了一个 UIActivityViewController,点击它应该会共享名为 sharedData 的数组的内容。
单击按钮时,共享选项弹出窗口为空。
这是我的函数代码:
@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)
}
这是我的全部 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
}
}
您正在另一个数组中使用一个字符串数组。您的 sharedData 是一个数组。
let shareItems: [Any] = [sharedData]
尝试使用此代码:
let shareItems: [Any] = sharedData