Search code examples
iosswiftswift3eureka-forms

Eureka row to present view controller and return value in Swift 3.0


I am looking for help in figuring out how to have a row in a MultivaluedSection present a view controller with a second Eureka form and return a value back to the MultivaluedSection row. I've been able to get a regular ButtonRow to push a view controller using a segue, but I can't figure out not to get a value back to the row in the MultivaluedSection. I'm not sure if the ButtonRow method supports returning values or not so I started looking for other solutions. One I found is to use a custom presenter row (https://github.com/xmartlabs/Eureka#custom-presenter-rows), but I don't understand how to make that work.

Here one thing I did find, but again, I don't understand how to put this all together:

Help creating simple Custom Presenter Row - https://github.com/xmartlabs/Eureka/issues/716

Can someone either point me to a working sample or help walk me through getting this setup?


Solution

  • If you are already pushing a new VC with a segue, then you might want to implement a protocol and define the functions to pass data back.

    Here is a good tutorial with Navigation controllers where at the end a Protocol is added.

    eg:

    View one (could be the Form View Controller with the 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
            }
        }
    }
    

    View two (the pushed View controller)

    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
        }
    }