2017-06-16 56 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 

    } 

    .... 
}