gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:33:00 25 4
gpt4 key购买 nike

我试图将这个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com