Ios 如何通过单击带有UITableViewCell类的.xib文件内的按钮来传输UIViewController?
我有一个Ios 如何通过单击带有UITableViewCell类的.xib文件内的按钮来传输UIViewController?,ios,swift,uitableview,uinavigationcontroller,Ios,Swift,Uitableview,Uinavigationcontroller,我有一个UIViewController,只有很少的tableview。每个单元格都是一个.xib文件,它继承了自己的类UITableViewCell。其中一个单元格有一个用于注销的按钮 我需要的是:单击按钮时,将viewController传输到另一个视图,即loginViewController 问题是:我无法访问我的tableViewCell类中的storyboard和navigationController。(如下) 您可以使用以下方式与视图控制器通信: “委派是一种设计模式,使类或结构
UIViewController
,只有很少的tableview
。每个单元格都是一个.xib文件,它继承了自己的类UITableViewCell
。其中一个单元格有一个用于注销的按钮
我需要的是:单击按钮时,将viewController
传输到另一个视图,即loginViewController
问题是:我无法访问我的tableViewCell
类中的storyboard
和navigationController
。(如下)
您可以使用以下方式与视图控制器通信: “委派是一种设计模式,使类或结构能够将其部分职责移交(或委派)给另一类型的实例。” 例如:
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
,您可以使用以下方式与视图控制器通信:
“委派是一种设计模式,使类或结构能够将其部分职责移交(或委派)给另一类型的实例。”
例如:
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
,您也可以尝试关闭
来实现这一点:
在表格视图单元格中
:
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
}
您也可以尝试
closure
来实现这一点:
在表格视图单元格中
:
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
}
在这篇文章中,您可以找到一个关于委托如何工作的很好的解释:您可以在这篇文章中找到一个关于委托如何工作的很好的解释:“我无法访问我的tableViewCell类中的storyboard和navigationController”,这无关紧要。按钮只是一个消息发送者。重要的是何时以及如何为按钮指定目标和操作,以及该目标是什么。“我无法访问tableViewCell类中的storyboard和navigationController”,这无关紧要。按钮只是一个消息发送者。重要的是什么时候以及如何给按钮指定目标和操作,以及该目标是什么。使用闭包是一个很好的解决方案。工作完美。谢谢你,阿米特。我现在正在使用你的答案,所以这是公认的答案;)。然而,我真的很喜欢@Francesco Deliro的答案。使用闭包是一个很好的解决方案。工作完美。谢谢你,阿米特。我现在正在使用你的答案,所以这是公认的答案;)。不过我真的很喜欢@Francesco Deliro的回答。