从用作 tableHeaderView 的 xib 文件中的按钮转到 VC

go to VC from button inside xib file that is used as a tableHeaderView

我有一个 TableSectionHeader xib 文件,里面有 button(和一些其他东西)。这个 xib 文件在我的 PostViewController.

中用作 UItableviewcustom header

我希望能够单击该按钮以显示有关该单元格的详细信息。但是,由于按钮位于 xib 文件中,因此 IBAction 位于 TableSectionHeader.Swift 中(继承自 UITableViewHeaderFooterView)。这意味着我不能segue or instantiate一个VC.

如何从 xib 文件中的这个按钮转到另一个 VC?

您需要将按钮的出口插入 xib class 而不是动作,当您在视图控制器中创建和 return header 时,您将拥有在 return 和 header 之前引用您的按钮,然后 addTarget 到您的按钮,并使用选择器标记到将处理转到下一个视图控制器的方法。

在您的 xib class 中拖动并连接按钮:

class YearSectionHeader : UITableViewHeaderFooterView {

@IBOutlet car button :UIButton!

}

在您的视图控制器中 table 查看 header 方法(示例)不要忘记将 class 和标识符更改为您使用的正确标识符:

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

        let header : YearSectionHeader = tableView.dequeueReusableHeaderFooterViewWithIdentifier("TableHeader") as! YearSectionHeader
        header.button.addTarget(self, action: #selector(self.handlingMethodName(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        header.button.tag = section
        return header

    }

sender.tag 是你的节号,你可以在这里执行 segue :

func handlingMethodName(sender:UIButton){

        print(sender.tag)

    }