我有一个 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/