我有一个问题,我正在开发一个应用程序,该应用程序显示了我以前的ViewController(FilterVC)
的{{1}}。我将过滤器数据存储在这样的委托方法中
ViewController(ItemsVC)
并单击按钮,我成功地将数据传递给了我以前的protocol FilterVCDelegate: class {
func didSelectedFilters(_ sender: FilterVC, with selectedFilters: [String:Any])
}
,如下所示:-
ItemVC
ItemVC:-
@IBAction func applyButtonAction(_ sender: UIButton) {
printD(selectedFilters)
self.delegate?.didSelectedFilters(self, with: self.selectedFilters)
self.dismiss(animated: true, completion: nil)
}
这是在调用api方法:-
func didSelectedFilters(_ sender: FilterVC, with selectedFilters: [String : Any]) {
self.currentFilters = selectedFilters
printD("selectedFilters \(currentFilters)") // successfully get data from previous vc
}
但是在func searchFromLatestRelease() {
// let param: [String:Any] = ["page": pageNo, "search_term": forSearch, "order_by": "date", "token": commonClass.sharedInstance.userToken ?? ""]
let param: [String:Any] = ["price_range": currentFilters["price"] as? [String] as Any, "page": "0", "is_rating": currentFilters["Product Rating"] as? [String] as Any]
printD("param: \(param)")
Alamofire.request(Constants.API.url("search"), method: .post, parameters: param, encoding: URLEncoding.httpBody, headers: nil).responseJSON {
(response:DataResponse<Any>) in
guard let json = response.result.value as? [String:Any] else {return}
printD(json)
guard let status = json["status"] as? Int else {return}
printD(status)
if status == 1 {
if self.pageNo == 0 { self.sneakers = [] }
guard let data = json["data"] as? [[String:Any]] else { return}
printD(data)
for dic in data {
self.sneakers.append(sneakerModel(response: dic))
}
self.reloadData()
}
else {
commonClass.sharedInstance.showErrorMessage(json["msg"] as? String ?? "Server error. Please try again later.")
}
}
}
和ViewWillAppear
中称呼它是行不通的。
现在,我需要使用这些文件管理器作为参数来调用 API 。刷新数据并成功在ViewDidLoad
上显示过滤的项目。 ViewController LifeCycle中有8个ItemsVC
,但我不是用来刷新我的ViewControllers
来显示过滤后的数据的。请帮忙吗?
这是我在控制台中得到的currenFilter,如下所示:-
ItemVC
(lldb)
答案 0 :(得分:1)
viewDidLoad
在启动vc时调用一次,ViewWillAppear
/ viewDidAppear
在模型关闭后调用,您需要
func didSelectedFilters(_ selectedFilters: [String : Any]) {
self.currentFilters = selectedFilters
self.searchFromLatestRelease()
}