neoneye / SwiftyFORM

iOS framework for creating forms
MIT License
1.08k stars 87 forks source link

ViewControllerFormItem and prepareForSegue #37

Closed hatembr closed 7 years ago

hatembr commented 7 years ago

Hi!

Is there a way to configure a view controller form item with a custom action so that I can perform a segue and prepare my destination view controller (pass state etc.)?

Thanks

neoneye commented 7 years ago

Hi @hatembr

You can do something like this.

class StoryboardDemoViewController: FormViewController {
    override func populate(_ builder: FormBuilder) {
        builder.navigationTitle = "Storyboard Demo"
        builder += voteButton
    }
    lazy var voteButton: ButtonFormItem = {
        let instance = ButtonFormItem()
        instance.title = "Vote"
        instance.action = { [weak self] in
                        // ... prepare state here ...
            let vc = VoteViewController.create(state: nil)
            self?.navigationController?.pushViewController(vc, animated: true)
        }
        return instance
    }()
}

How does this work for you?

neoneye commented 7 years ago

Hi @hatembr

I have made an example that shows how to pass data to a storyboard.

It's available on the developer branch.

See this commit https://github.com/neoneye/SwiftyFORM/commit/9928278f12717932eefb99c87ace86b3aadb74de

hatembr commented 7 years ago

Hi @neoneye

Thanks a lot for this example. Actually it works and solves the issue of passing data to the pushed VC. The only thing is that it's a Button form item instead of a View Controller form item which has the accessory enabled and a different visual style. I think it would be a very good addition to be able to pass a custom action to the view controller form item as well. Either via a completion handler or whatever allows the developer to have a custom code triggering the transition to the destination VC like pushing a VC or performing a segue.

neoneye commented 7 years ago

That is a great suggestion and easy to implement. I will consider this next time I find time.

Thank you @hatembr

hatembr commented 7 years ago

you're welcome 👍

justdan0227 commented 3 years ago

Here is another example of how to do it with a Storyboard ID

lazy var paymentInfoButton: ButtonFormItem = {
            let instance = ButtonFormItem()
            instance.title = "Payment Info"
            instance.action = { [weak self] in
                let storyboard = UIStoryboard(name: "More", bundle: nil) // StoryboardName
                let controller = storyboard.instantiateViewController(withIdentifier: "PaymentInfoSB") as? PaymentMethodViewController
                if (controller != nil) {
                    controller!.x = y // Set property inside of view controller 
                    self?.navigationController?.pushViewController(controller!, animated: true)
                }
            }
            return instance
        }()