HeaderView 按钮以编程方式转到另一个视图控制器
HeaderView Button Segue to Another View Controller Programmatically
我有一个带有按钮的原型 header 视图,我希望能够单击该按钮以转到另一个视图控制器 (TargetViewController
)。
但是,由于某种原因,我无法让它工作。我正在尝试以编程方式完成这一切,因此没有像我通常习惯的 prepareForSegue
函数。你能告诉我我做错了什么吗?
如果使用委托,我还需要 addTarget
吗?而且我认为最大的问题是我无法弄清楚我应该将哪个 class 设置为 delegate
等于 .
我在这方面还很陌生,所以非常感谢任何帮助!
相关代码行如下:
protocol HeaderCellDelegate: class {
func didTapEdit()
}
class HeaderCell: UITableViewHeaderFooterView {
var editButton: UIButton!
weak var delegate: HeaderCellDelegate?
override init(reuseIdentifier: String?) {
self.editButton = UIButton()
self.editButton.setImage(UIImage(named: "Edit"), for: .normal)
super.init(reuseIdentifier: reuseIdentifier)
self.editButton.addTarget(self, action: #selector(editButtonPressed), for: .touchUpInside)
self.contentView.addSubview(editButton)
// Plaeholder for constraints
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
@objc func editButtonPressed() {
delegate?.didTapEdit()
print("delegate didTapEdit")
}
}
Table 视图控制器:
extension ProfileDataController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return tableViewSections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewSections[section].rows.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
return header
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UITableViewCell
return cell
}
}
目标视图控制器:
import UIKit
extension TargetViewController: HeaderCellDelegate {
func didTapEdit() {
print("editButtonPressed")
}
}
您需要将委托设置为当前视图控制器(在您的情况下,您需要像下面这样设置)
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
header.deelgate = self
return header
}
然后在您的 ProfileDataController
class 中,您需要实现 HeaderCellDelegate
方法,如下所示
extension ProfileDataController : HeaderCellDelegate {
let viewControllerToBePushed = TargetViewController() //get the `TargetViewController` here...
self.navigationController.pushViewController(viewControllerToBePushed, animated: true)
}
所以TargetViewController
会被推过去ProfileDataController
你需要像 header.delegate = self
一样给 HeaderCellDelegate
代表
extension ProfileDataController: UITableViewDataSource, UITableViewDelegate,HeaderCellDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
header.delegate = self
return header
}
}
然后在ProfileDataController
中实现删除方法,通过使用performSegue
方法你可以执行你的Segue
//HeaderCellDelegate
extension ProfileDataController {
func didTapEdit() {
print("editButtonPressed")
self.performSegue(withIdentifier: "YOUR SEGUE IDENTIFIRE", sender: nil)
}
}
没有委托,您可以在 ProfileDataController
中添加操作
extension ProfileDataController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
header.editButton.addTarget(self, action: #selector(editButtonPressed), for: .touchUpInside)
return header
}
}
在ProfileDataController
@objc func editButtonPressed() {
print("edit button pressed")
self.performSegue(withIdentifier: "YOUR SEGUE IDENTIFIRE", sender: nil)
}
我有一个带有按钮的原型 header 视图,我希望能够单击该按钮以转到另一个视图控制器 (TargetViewController
)。
但是,由于某种原因,我无法让它工作。我正在尝试以编程方式完成这一切,因此没有像我通常习惯的 prepareForSegue
函数。你能告诉我我做错了什么吗?
如果使用委托,我还需要 addTarget
吗?而且我认为最大的问题是我无法弄清楚我应该将哪个 class 设置为 delegate
等于 .
我在这方面还很陌生,所以非常感谢任何帮助!
相关代码行如下:
protocol HeaderCellDelegate: class {
func didTapEdit()
}
class HeaderCell: UITableViewHeaderFooterView {
var editButton: UIButton!
weak var delegate: HeaderCellDelegate?
override init(reuseIdentifier: String?) {
self.editButton = UIButton()
self.editButton.setImage(UIImage(named: "Edit"), for: .normal)
super.init(reuseIdentifier: reuseIdentifier)
self.editButton.addTarget(self, action: #selector(editButtonPressed), for: .touchUpInside)
self.contentView.addSubview(editButton)
// Plaeholder for constraints
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
@objc func editButtonPressed() {
delegate?.didTapEdit()
print("delegate didTapEdit")
}
}
Table 视图控制器:
extension ProfileDataController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return tableViewSections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewSections[section].rows.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
return header
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UITableViewCell
return cell
}
}
目标视图控制器:
import UIKit
extension TargetViewController: HeaderCellDelegate {
func didTapEdit() {
print("editButtonPressed")
}
}
您需要将委托设置为当前视图控制器(在您的情况下,您需要像下面这样设置)
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
header.deelgate = self
return header
}
然后在您的 ProfileDataController
class 中,您需要实现 HeaderCellDelegate
方法,如下所示
extension ProfileDataController : HeaderCellDelegate {
let viewControllerToBePushed = TargetViewController() //get the `TargetViewController` here...
self.navigationController.pushViewController(viewControllerToBePushed, animated: true)
}
所以TargetViewController
会被推过去ProfileDataController
你需要像 header.delegate = self
HeaderCellDelegate
代表
extension ProfileDataController: UITableViewDataSource, UITableViewDelegate,HeaderCellDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
header.delegate = self
return header
}
}
然后在ProfileDataController
中实现删除方法,通过使用performSegue
方法你可以执行你的Segue
//HeaderCellDelegate
extension ProfileDataController {
func didTapEdit() {
print("editButtonPressed")
self.performSegue(withIdentifier: "YOUR SEGUE IDENTIFIRE", sender: nil)
}
}
没有委托,您可以在 ProfileDataController
extension ProfileDataController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
header.editButton.addTarget(self, action: #selector(editButtonPressed), for: .touchUpInside)
return header
}
}
在ProfileDataController
@objc func editButtonPressed() {
print("edit button pressed")
self.performSegue(withIdentifier: "YOUR SEGUE IDENTIFIRE", sender: nil)
}