Swift 委托协议未调用
Swift delegate protocol not calling
我是 Swift 的初学者。我正在尝试使用委托和协议将数据从子视图向后发送到根视图(在导航控制器中)
这是我的代码:
//FilterViewController.swift:
protocol FilterDelegate: class {
func finishFilter(query: String);
}
class FilterViewController:BaseViewController,....{
....
weak var delegate : FilterDelegate?
@IBAction func acceptTapped(_ sender: UIButton) {
var querystring = ""
var conditions: [String] = []
//some logic works with conditions
querystring = conditions.joined(separator: "&")
self.delegate?.finishFilter(query: querystring)
self.navigationController?.popViewController(animated: true)
}
}
这里是我从 Filter 调用委托的地方
//HouseListController.swift
class HouseListController: BaseViewController,..{
var filterController = FilterViewController()
//Here is where I push the FilterViewController
@IBAction func filterTapped(_ sender: UIButton) {
self.currentMode = .filter
self.tracking.previousMode = .filter
self.filterController = storyboard?.instantiateViewController(withIdentifier: "FilterView") as! FilterViewController
self.navigationController?.pushViewController(self.filterController, animated: true)
}
override func viewDidLoad() {
self.filterController.delegate = self
}
}
extension HouseListController : FilterDelegate {
func finishFilter(query: String) {
switch self.currentMode{
case .normal:
_ = self.filterHouse(querystring: query , offset: 0, limit: self.tracking.limit).done{ dataHouses in
self.houses.filter = dataHouses
self.houses.defaultHouses = dataHouses
}.done{ _ in
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
case .search:
_ = self.filterHouse(searchKey: self.tracking.search.searchKey, querystring: query, offset: 0, limit: self.tracking.limit).done{ dataHouses in
self.houses.filter = dataHouses
self.houses.defaultHouses = dataHouses
}.done{ _ in
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
default:
_ = self.filterHouse(querystring: query , offset: 0, limit: self.tracking.limit).done{ dataHouses in
self.houses.filter = dataHouses
self.houses.defaultHouses = dataHouses
}.done{ _ in
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
self.currentMode = .filter
}
}
代理没有调用我的 finishFilter 函数。相反,当我在 FilterController 中弹出视图时,它会直接转到 HouseListController 中的 viewWillAppear 并停留在那里而没有调用任何委托。
我是不是漏掉了什么?
您在 filterTapped
中显示的代码是问题所在。您从情节提要中创建了 FilterViewController
的新实例,但您从未设置该实例的 delegate
属性.
我会摆脱你的 filterController
属性。删除 viewDidLoad
中设置其 delegate
的行。您从未使用过该代码,因此不需要它。
然后更新filterTapped
:
@IBAction func filterTapped(_ sender: UIButton) {
self.currentMode = .filter
self.tracking.previousMode = .filter
let filterController = storyboard?.instantiateViewController(withIdentifier: "FilterView") as! FilterViewController // Update this line
filterController.delegate = self // Add this line
self.navigationController?.pushViewController(self.filterController, animated: true)
}
如果要向后发送数据,请使用闭包。假设你有 2 viewcontrollers A & B,你想要从 B 到 A 的数据,在 B viewcontroller.
中声明闭包
class B_ViewController: UIViewController{
var closureData: ((NSMutableArray) -> Void) = { arg in }
@IBAction func onBtnDoneClick(_ sender: Any) {
self.closureData(self.selectedClosureArray)
self.navigationController?.popViewController(animated: true)
}
}
现在在视图控制器A中调用在视图控制器B中声明的闭包。
这里A_VC_array
是NSMutableArray
,bVC
是ViewcontrollerB的对象
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.bVC.closureData = { (selectedClosureData:NSMutableArray) -> Void in
self.A_VC_array = selectedClosureData
}
}
我是 Swift 的初学者。我正在尝试使用委托和协议将数据从子视图向后发送到根视图(在导航控制器中)
这是我的代码:
//FilterViewController.swift:
protocol FilterDelegate: class {
func finishFilter(query: String);
}
class FilterViewController:BaseViewController,....{
....
weak var delegate : FilterDelegate?
@IBAction func acceptTapped(_ sender: UIButton) {
var querystring = ""
var conditions: [String] = []
//some logic works with conditions
querystring = conditions.joined(separator: "&")
self.delegate?.finishFilter(query: querystring)
self.navigationController?.popViewController(animated: true)
}
}
这里是我从 Filter 调用委托的地方
//HouseListController.swift
class HouseListController: BaseViewController,..{
var filterController = FilterViewController()
//Here is where I push the FilterViewController
@IBAction func filterTapped(_ sender: UIButton) {
self.currentMode = .filter
self.tracking.previousMode = .filter
self.filterController = storyboard?.instantiateViewController(withIdentifier: "FilterView") as! FilterViewController
self.navigationController?.pushViewController(self.filterController, animated: true)
}
override func viewDidLoad() {
self.filterController.delegate = self
}
}
extension HouseListController : FilterDelegate {
func finishFilter(query: String) {
switch self.currentMode{
case .normal:
_ = self.filterHouse(querystring: query , offset: 0, limit: self.tracking.limit).done{ dataHouses in
self.houses.filter = dataHouses
self.houses.defaultHouses = dataHouses
}.done{ _ in
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
case .search:
_ = self.filterHouse(searchKey: self.tracking.search.searchKey, querystring: query, offset: 0, limit: self.tracking.limit).done{ dataHouses in
self.houses.filter = dataHouses
self.houses.defaultHouses = dataHouses
}.done{ _ in
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
default:
_ = self.filterHouse(querystring: query , offset: 0, limit: self.tracking.limit).done{ dataHouses in
self.houses.filter = dataHouses
self.houses.defaultHouses = dataHouses
}.done{ _ in
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
self.currentMode = .filter
}
}
代理没有调用我的 finishFilter 函数。相反,当我在 FilterController 中弹出视图时,它会直接转到 HouseListController 中的 viewWillAppear 并停留在那里而没有调用任何委托。
我是不是漏掉了什么?
您在 filterTapped
中显示的代码是问题所在。您从情节提要中创建了 FilterViewController
的新实例,但您从未设置该实例的 delegate
属性.
我会摆脱你的 filterController
属性。删除 viewDidLoad
中设置其 delegate
的行。您从未使用过该代码,因此不需要它。
然后更新filterTapped
:
@IBAction func filterTapped(_ sender: UIButton) {
self.currentMode = .filter
self.tracking.previousMode = .filter
let filterController = storyboard?.instantiateViewController(withIdentifier: "FilterView") as! FilterViewController // Update this line
filterController.delegate = self // Add this line
self.navigationController?.pushViewController(self.filterController, animated: true)
}
如果要向后发送数据,请使用闭包。假设你有 2 viewcontrollers A & B,你想要从 B 到 A 的数据,在 B viewcontroller.
中声明闭包class B_ViewController: UIViewController{
var closureData: ((NSMutableArray) -> Void) = { arg in }
@IBAction func onBtnDoneClick(_ sender: Any) {
self.closureData(self.selectedClosureArray)
self.navigationController?.popViewController(animated: true)
}
}
现在在视图控制器A中调用在视图控制器B中声明的闭包。
这里A_VC_array
是NSMutableArray
,bVC
是ViewcontrollerB的对象
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.bVC.closureData = { (selectedClosureData:NSMutableArray) -> Void in
self.A_VC_array = selectedClosureData
}
}