ios - 如何从自定义 TableViewCell 访问 ViewController 的 navigationController

标签 ios swift uitableview

我有一个 UITableView,我想为其实现自定义 header 。在自定义标题中,我想要一个按钮,当点击该按钮时,会将新 View 推送到导航 Controller 上。我在访问自定义 header View 的 super ViewController 中的 navigationController 时遇到问题。

class CustomViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

    //setup views and whatnot
    //setup delegate and datasource


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

class CustomHeaderView : UIView {
    let customHeaderButton : UIButton = {
        let button = UIButton()
        ...
        button.addTarget(self, action: #selector(handleCustomHeaderButtonAction()), for: .touchUpInside)
        return button
    }()
    ...
    func handleCustomHeaderButtonAction(){
        //push a new view onto the CustomViewController's navigationController
        ???
}

最佳答案

试试这个:

class SecondViewController: UIViewController,ChildDelegate {


    internal func navigatToCustomViewController() {
        // Code to write push yur controller
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        let customview = customView()
        customview.delegate = self

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

// A Protocol
protocol ChildDelegate: class {
    func navigatToCustomViewController()
}

// You custom view
class customView : UIView {

    weak var delegate: ChildDelegate?

    func actionDetailClick() {
        delegate?.navigatToCustomViewController()
    }
}

关于ios - 如何从自定义 TableViewCell 访问 ViewController 的 navigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40168808/

相关文章:

swift - 5 秒后开始过渡

ios - XCode UITable 滚动更改文本值

iphone - 当带有核心数据的 UITableView 部分的最后一行被移动时 -> NSRangeException

ios - 如何显示来自不同类的警报

ios - ScrollView - 手势识别器 - 垂直滑动

ios - 是否可以在应用程序图标上显示进度

ios - 如何将元组字典保存和读取到 NSUserDefaults?

ios - 在 UITableViewCell 中检测具有灵活宽度的 View 的最终布局大小

iphone - 加载 NIB 时抛出 NSInternalConsistencyException

ios - 在 ViewControllerB 到 ViewControllerA 之间传递数据,委托(delegate)不会被调用