HeaderView按钮以编程方式查看另一个视图控制器

时间:2018-02-16 06:28:50

标签: ios swift uitableview swift4 swift-protocols

我有一个原型标题视图,其中包含一个按钮,我希望能够单击该按钮以转到另一个视图控制器(TargetViewController)。

但是,由于某种原因,我还没能把它上班。我试图以编程方式执行此操作,因此没有像我通常习惯的prepareForSegue函数。你能告诉我我做错了吗?

如果使用代表,我还需要addTarget吗?我认为最大的问题是我无法弄清楚应该将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")
    }
}

表视图控制器:

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")
    }
}

3 个答案:

答案 0 :(得分:1)

您需要将委托设置为当前视图控制器(在您的情况下,您需要设置如下)

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
header.deelgate = self
    return header
}

然后在ProfileDataController课程中,您需要实施HeaderCellDelegate方法,如下所示

extension ProfileDataController : HeaderCellDelegate {

  let viewControllerToBePushed = TargetViewController() //get the `TargetViewController` here...
  self.navigationController.pushViewController(viewControllerToBePushed, animated: true)
}

因此TargetViewController将被推过ProfileDataController

答案 1 :(得分:1)

如果没有代理人,您可以在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)
}

答案 2 :(得分:0)

您需要提供"feathr"

之类的HeaderCellDelegate代表
header.delegate = self

然后在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方法执行您的Segue

performSegue
相关问题