2017-06-16 5 views
0

このSuperTViewController.swiftをベースビューコントローラとして使用して、戻るボタンのデリゲートを設定しようとしていました。 ProjectTableViewController.swiftは、デリゲートを適用するViewControllerの1つです。 ProjectTableViewController.swiftは実際には拡張クラスのためボタンを持っています。しかし、私は別のページごとに異なるアクションのカスタムバック()をしたい。実行時には 、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 

    } 

    .... 
}