我试图将此SuperTViewController.swift用作基本视图控制器并为后退按钮设置委托。 ProjectTableViewController.swift是应用委托的viewcontrollers之一。由于扩展类,ProjectTableViewController.swift确实有按钮。但是我希望自定义back()用于每个不同页面的不同操作。谈到执行 ,print(“project”)无法运行。请告诉我实施代表的详细信息并申请?以下是我的代码。
SuperTViewController.swift
protocol backProtocol : UINavigationBarDelegate {
func back ()
}
class SuperTViewController: UITableViewController {
weak var delegate: backProtocol?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "btn_add")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(ButtonTapped))
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
func ButtonTapped() {
print("Button Tapped")
delegate?.back()
}
}
ProjectTableViewController.swift
import UIKit
class ProjectTableViewController: SuperTViewController {
override func viewDidLoad() {
super.viewDidLoad()
delegate?.back()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
func back () {
print("project")
}
}
答案 0 :(得分:1)
首先,扩展(实现)协议。然后,将委托分配给自己
{{1}}