gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:38:32 25 4
gpt4 key购买 nike

我有一个 UITableView,我想为其实现自定义 header 。在自定义 header 中,我想要一个按钮,当点击时将新 View 推送到 navigationController 上。我无法访问自定义 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 - 如何从自定义 UITableViewCell 访问 ViewController navigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40168808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com