2017-06-16 60 views
0

我試圖使用這個SuperTViewController.swift作爲基本視圖控制器併爲後退按鈕設置委託。 ProjectTableViewController.swift是應用代理的視圖控制器之一。 ProjectTableViewController.swift確實具有擴展類的按鈕。但是我想爲每個不同頁面的不同動作定製back()。當涉及到執行 時,print(「project」)無法運行。請您告訴我實施代表的細節並申請嗎?以下是我的代碼。SWIFT 3.0委託導航欄按鈕

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") 
    } 


} 

回答

1

首先,延伸(實施)的protocol.Then,分配委託給自

class ProjectTableViewController: SuperTViewController, UINavigationBarDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     delegate = self 

    } 

    .... 
}