【问题标题】:Can't pass datas back from UIView Popover Dismiss无法从 UIView Popover Dismiss 传回数据
【发布时间】:2019-09-15 14:28:29
【问题描述】:

我在按钮的 clic 上激活的 UIView (A) 和另一个 UIView (B)(嵌入在导航控制器中)之间设置了“显示为弹出框”segue。

当我关闭它时,我正试图将数据从 (B) 传递回 (A)(我想同时保持弹出动画)。

我尝试了很多我在这里找到的方法,主要是在 Stackoverflow 上,但到目前为止,我从未成功检索到 (A) 上的数据。

我尝试了委托和协议以及其他更简单的方法。最后一个日期如下:

  • 在 (A) 中,我只是尝试打印应该在 ViewWillAppear 中存储数据的变量:
class SearchBarsController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

    var testValue:String = ""

    override func viewWillAppear(_ animated: Bool) {
    print(testValue) // print is empty
    super.viewWillAppear(animated)
    }

}
  • 在 (B) 中,我关闭弹出框并尝试通过按钮 clic 将数据发送回:
class SearchFilterViewController: UIViewController {

    @IBAction func DismissPopoverOnClic(_ sender: Any) {
            if let navController = presentingViewController as? UINavigationController {
                let presenter = navController.topViewController as! SearchBarsController
                presenter.testValue = "Test"
                print("success") //never called
            }
            self.dismiss(animated: true, completion: nil)
    }

}

在 (B) 上,我想设置一些过滤器,以便在 (A) 上使用以在表格视图中显示搜索结果。但实际上testValue的值总是空白的。

【问题讨论】:

  • 您可以使用 Unwind Segue 从视图控制器 B 回到 A。如果您愿意,我可以为您制作一个示例项目。

标签: ios swift xcode segue popover


【解决方案1】:

好的,所以你可以使用 unwind segue 来完成,这里是示例项目:

sample projecct

过程:

将此方法添加到SearchBarsController下方viewWillAppear

@IBAction func unWindFromFilterViewController(_ sender: UIStoryboardSegue) {

    }

转到 Storyboard 并转到 SearchFilterViewController,然后 cntrl + 从 DismissPopoverOnClic 拖动到退出按钮的顶部,然后选择 unWindFromFilterViewController。

SearchFilterViewController写这个传递数据的方法

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destVC = segue.destination as? ViewController {
           destVC.testValue = "Test"

        }
    }

您将获得所需的数据。谢谢

【讨论】:

  • 非常感谢。像魅力一样工作,绝对是我发现的最简单的方法。
【解决方案2】:

将数据传回viewController 时,使用delegate 实现它的最有效方式

protocol SearchFilterViewControllerDelegate {
    func setTextValue(string : String)
}

class SearchFilterViewController: UIViewController {

    var delegate : SearchFilterViewControllerDelegate?


    @IBAction func DismissPopoverOnClic(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
        delegate?.setTextValue(string : "Test Value")
    }
}

class SearchBarsController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

    var filterViewController : SearchFilterViewController? 
    func popup() {
        // your pop up code and init filterViewController
        filterViewController.delegate = self **//without this line, the delegate will be nil, no nothing will happen.**
    }
}

extension SearchBarsController : SearchFilterViewControllerDelegate {
    func setTextValue(string : String) {
        print(string)
    }
}

【讨论】:

    猜你喜欢
    • 2013-05-01
    • 2019-04-28
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2017-12-01
    • 2021-05-04
    相关资源
    最近更新 更多