ios - 当我们快速从 Presented ViewController 转到 Previous ViewController 时调用哪个方法

标签 ios swift delegates

我有一个问题,我正在开发一个应用程序,在该应用程序中,我展示了一个来 self 之前的 ViewController(ItemsVC)ViewController(FilterVC)。我像这样将过滤器数据存储在我的委托(delegate)方法中

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

但它不会工作在 ViewWillAppearViewDidLoad 中调用它。 现在我需要使用这些文件管理器作为调用 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"]]

(数据库)

最佳答案

viewDidLoad在vc启动时调用一次,ViewWillAppear/viewDidAppear在model dismiss后调用,需要

func didSelectedFilters(_ selectedFilters: [String : Any]) {
    self.currentFilters = selectedFilters
    self.searchFromLatestRelease()
}

关于ios - 当我们快速从 Presented ViewController 转到 Previous ViewController 时调用哪个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53131637/

相关文章:

ios - 圆角 UITableViewCell (iOS)

ios - 相当于 Objective-c 中的 Swift Public 方法?

swift - 如何在 swift 3 中使用 UnsafeMutablePointer<T?>

c# - 通用委托(delegate),C# 3.5

ios - 使用委托(delegate)将数据从一个 TableView 传输到另一个

ios - 加载新 VC 时自动选择文本字段

ios - 在 UISearchController 中搜索时无法在 tableView 中滚动

iOS 崩溃 : Terminating app due to uncaught exception reason: UIPopoverPresentationController should have a non-nil sourceView

ios - 删除核心数据中的记录后无法删除索引处的表行

C#编译错误: "Invoke or BeginInvoke cannot be called on a control until the window handle has been created."