在Swift 3.0中,如何将Eureka行呈现给视图控制器并返回值?

3
我需要帮助来解决如何让MultivaluedSection中的一行展现一个视图控制器并返回一个值给MultivaluedSection中的该行。我已经能够使用segue使普通的ButtonRow推出一个视图控制器,但是我无法弄清楚如何将值传回到MultivaluedSection中的该行。我不确定ButtonRow方法是否支持返回值,因此开始寻找其他解决方案。我发现一个解决方法是使用自定义presenter row (https://github.com/xmartlabs/Eureka#custom-presenter-rows),但我不知道如何让它工作。
我找到了一个东西,但同样不知道如何将这些组合起来:
帮助创建简单的自定义Presenter行 - https://github.com/xmartlabs/Eureka/issues/716 可以有人指向一个可用的示例或帮助我完成设置吗?

我发现另一个选项是使用ButtonRowWithPresent,但我还是无法弄清楚。 - Jason
1个回答

4
如果您已经使用segue推送了一个新的VC,那么您可能需要实现一个协议并定义函数来传递数据回来。 这里是一个很好的导航控制器教程,在最后添加了一个协议。
例如:

视图一(可以是带有ButtonRow的表单视图控制器)

class FormVC: FormViewController , FooViewControllerDelegate{
    var text : String!
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    /// Delegate protocol callback implementation
    func myVCDidFinish(controller: FooViewController, text: String) {
        // Receive the data as a delegate
        self.text = text
        // In this case we also want to finish the view
        controller.navigationController?.popViewController(animated: true)
    }

    /// This represents the prepare for segue mentioned as implemented in the question
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Act upon the segue we want from this VC
        // The string is defined in the storyboard, so it must be exactly the same
        if segue.identifier == "mySegue"{
            // Creating the second VC instance
            let vc = segue.destination as! FooViewController
            // Since this class is now a delegate, setup the delegate
            vc.delegate = self
        }
    }
}

视图二(已推出的视图控制器)

protocol FooViewControllerDelegate {
    func myVCDidFinish(controller: FooViewController, text: String)
}

class FooViewController: UIViewController {
    /// Data
    var text : String!

    /// Set up an optional delegate
    var delegate:FooViewControllerDelegate? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        // Init label
        self.text = "Pushed view data to pass back
    }
}

因为之前的回答被删除了,所以再次添加一个答案。如果它解决了问题,请接受答案。 - Efren

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接