【问题标题】:how to call presentViewController from within a UICollectionViewCell如何从 UICollectionViewCell 中调用 presentViewController
【发布时间】:2016-10-18 15:03:09
【问题描述】:

UIViewController 调用此函数不会出现问题,但从UICollectionViewCell 调用它会引发预编译错误

功能:

func didTapShare(sender: UIButton)
{
    let textToShare = "Swift is awesome!  Check out this website about it!"

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

        activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]

        activityVC.popoverPresentationController?.sourceView = sender
        self.presentViewController(activityVC, animated: true, completion: nil)
    }
}

错误:

您的单元格没有成员 presentViewController

怎么办?

【问题讨论】:

  • 您应该将事件委托给您的视图控制器并从那里呈现一个新的。
  • 你能举个例子吗?
  • 我已经发布了一个委托的例子,如果有什么不清楚的地方,请随时询问。

标签: ios swift


【解决方案1】:

UITableViewCell 永远不应该处理任何业务逻辑。它应该在视图控制器中实现。您应该使用委托:

UICollectionViewCell 子类:

protocol CustomCellDelegate: class {
    func sharePressed(cell: MyCell)
}

class CustomCell: UITableViewCell {
    var delegate: CustomCellDelegate?

    func didTapShare(sender: UIButton) {
        delegate?.sharePressed(cell: self)
    }
}

视图控制器:

class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    //...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomCell
        cell.delegate = self
        return cell
    }
}

extension TableViewController: CustomCellDelegate {
    func sharePressed(cell: CustomCell) {
        guard let index = tableView.indexPath(for: cell)?.row else { return }
        //fetch the dataSource object using index
    }
}

【讨论】:

  • 我花了最后一个小时来解决类似的问题。这个答案正是我所需要的。谢谢!
  • 我收到一个错误 无法将“ViewController”类型的值分配给“CustomCellDelegate”类型?插入 ' 作为! CustomCellDelegate' 上线cell.delegate = self。但是如果我插入as! CustomCellDelegate,程序就会在同一行崩溃。
  • @Emm 那是因为你的ViewController 没有实现CustomCellDelegate 协议。确保你有这个extension ViewController: CustomCellDelegate {...} 部分
【解决方案2】:

那是因为presentViewControllerUIViewController 方法,UITableViewCell 没有称为presentViewController 的方法。

怎么办?

您可以使用Delegation 模式来处理对按钮操作的访问(作为@alexburtnik 的回答),或者-为了节省一些额外的工作- 我建议通过 tag 识别它来处理 viewController 中单元格按钮的操作。

注意:Swift 3 代码。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell

        cell.myButton?.tag = indexPath.row
        cell.myButton?.addTarget(self, action: #selector(), for: .touchUpInside)

        return cell
    }

    func namesIsTapped(tappedButton: UIButton) {
        // get the user (from users array for example) by using the tag, for example:
        let currentUser = users[tappedButton.tag]

        // do whatever you want with this user now...

    }

希望有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    相关资源
    最近更新 更多