![](/img/trans.png)
[英]How to pass the data from modally presented ViewController to parent ViewController in swift 5?
[英]Which method is called when we go from Presented ViewController to Previous ViewController in swift
我有一個問題,我在其中我提出了一個應用程序的工作ViewController(FilterVC)
從我以前的ViewController(ItemsVC)
我將過濾器數據存儲在這樣的委托方法中
protocol FilterVCDelegate: class {
func didSelectedFilters(_ sender: FilterVC, with selectedFilters: [String:Any])
}
然后單擊按鈕,我將數據成功傳遞給我以前的ItemVC
如下所示:-
@IBAction func applyButtonAction(_ sender: UIButton) {
printD(selectedFilters)
self.delegate?.didSelectedFilters(self, with: self.selectedFilters)
self.dismiss(animated: true, completion: nil)
}
ItemVC: -
func didSelectedFilters(_ sender: FilterVC, with selectedFilters: [String : Any]) {
self.currentFilters = selectedFilters
printD("selectedFilters \(currentFilters)") // successfully get data from previous vc
}
這里是調用api方法:-
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
和ViewDidLoad
調用它是ViewDidLoad
。 現在,我需要使用這些文件管理器作為參數來調用API 。 刷新數據並成功在ItemsVC
上顯示篩選的項目。 ViewController LifeCycle中有8個ViewControllers
,但我不打算使用它來刷新ItemVC
以顯示過濾的數據。 請幫忙?
在這里,我在控制台中得到我的currenFilter,如下所示:-
selectedFilters ["Product Rating": ["5", "4", "3", "2", "1"], "Price": ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00", "$800.00 - $1000.00", "$1000.00 AND OVER"]]
(LLDB)
初始化vc時調用一次viewDidLoad
,模型關閉后調用ViewWillAppear
/ viewDidAppear
,您需要
func didSelectedFilters(_ selectedFilters: [String : Any]) {
self.currentFilters = selectedFilters
self.searchFromLatestRelease()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.