ios - 导航栏按钮的 SWift 3.0 委托(delegate)

标签 ios swift delegates

我试图将这个 SuperTViewController.swift 用作基本 View Controller 并为后退按钮设置委托(delegate)。 ProjectTableViewController.swift 是应用委托(delegate)的 View Controller 之一。由于扩展类,ProjectTableViewController.swift 确实有按钮。但是我想要为每个不同页面的不同操作自定义 back()。执行时 , print("project") 无法运行。你能告诉我实现委托(delegate)和申请的细节吗?以下是我的代码,。

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


}

最佳答案

首先,扩展(实现)协议(protocol)。然后,将委托(delegate)分配给自己

class ProjectTableViewController: SuperTViewController, UINavigationBarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        delegate = self

    }

    ....
}

关于ios - 导航栏按钮的 SWift 3.0 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44583933/

相关文章:

ios - UIScreen.main.bounds 是边到边吗?

ios - VPN - NEPacketTunnelProvider - 后台模式

ios - Swift 的 Spotify API

ios - 无法导入桥接头 facebook sdk

swift - 添加进度条 - Swift

c# - 如何接受任何委托(delegate)作为参数

ios - Xcode 9 beta4/5 : This app was unable to be installed

ios - 如何在 swift 中以编程方式实现具有多个原型(prototype)单元格的 UITableViewController

objective-c - Objective-C - 策略模式?

constructor - F# 使用构造函数作为函数