簡體   English   中英

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM