gpt4 book ai didi

ios - 如何通过单击具有 UITableViewCell 类的 .xib 文件内的按钮来传输 UIViewController?

转载 作者:行者123 更新时间:2023-11-28 10:37:13 25 4
gpt4 key购买 nike

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

我需要的是:当我点击按钮时,将 viewController 传输到另一个 View ,即 loginViewController

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

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 中:

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
}

关于ios - 如何通过单击具有 UITableViewCell 类的 .xib 文件内的按钮来传输 UIViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52800697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com