【问题标题】:How to transmit UIViewController by clicking on a button inside a .xib file with a class of UITableViewCell?如何通过单击具有 UITableViewCell 类的 .xib 文件中的按钮来传输 UIViewController?
【发布时间】:2019-03-18 22:26:03
【问题描述】:

我有一个 UIViewController 和几个 TableViews。每个单元格都是一个 .xib 文件,它自己的类继承 UITableViewCell。其中一个单元格有一个用于注销的按钮。

我需要的是:当我单击按钮时,将viewController 传输到另一个视图loginViewController。

问题是:我无法访问 tableViewCell 类中的 storyboard 和 navigationController。 (如下)

class ProfileCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    @IBAction func btnLogout(_ sender: Any) {

    //here I want to navigate to another view.

       //below is a sample when I use in viewController classes. but how to use it inside this class.

        let vc = self.storyboard?.instantiateViewController(withIdentifier: "LoginID") as! LoginViewController
        self.navigationController?.present(vc, animated: true, completion: nil)
    }

}

【问题讨论】:

  • “我无法访问我的 tableViewCell 类中的故事板和导航控制器”这没关系。该按钮只是一个消息发送者。重要的是您何时以及如何为按钮指定目标和操作,以及该目标是什么。

标签: ios swift uitableview uinavigationcontroller


【解决方案1】:

您也可以尝试closure 来实现:

在你的tableViewCell:

class ProfileCell: UITableViewCell {

    var callBackOnButtonLogout: (()->())?

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    @IBAction func btnLogout(_ sender: Any) {
        self.callBackOnButtonLogout?()
    }
}  

然后在你的cellForRowAt 方法中:

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

    cell.callBackOnButtonLogout = {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "LoginID") as! LoginViewController
        self.navigationController?.present(vc, animated: true, completion: nil)
    }

    return cell 
} 

【讨论】:

  • 使用闭包是一个不错的解决方案。完美运行。谢谢阿米特。
  • 我现在正在使用您的答案,所以这是公认的答案;)。不过我真的很喜欢@Francesco Deliro 的回答。
【解决方案2】:

您可以使用delegation pattern 与视图控制器通信:

“委托是一种设计模式,它使类或结构能够将其部分职责移交(或委托)给另一种类型的实例。”

例如:

protocol LogoutDelegate: class {
    func shouldLogout()
}

class ProfileCell: UITableViewCell {

    weak var logoutDelegate: LogoutDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    @IBAction func btnLogout(_ sender: Any) {
        logoutDelegate?.shouldLogout()
    }

}

然后你必须在你的视图控制器中采用协议并设置cell.logoutDelegate = self。

【讨论】:

  • 你可以在这篇文章中找到关于委托如何工作的很好的解释:Delegation in Swift
猜你喜欢
  • 1970-01-01
  • 2018-05-22
  • 1970-01-01
  • 2020-12-07
  • 2021-12-12
  • 2016-08-14
  • 2020-06-08
  • 2021-10-14
  • 1970-01-01
相关资源
最近更新 更多