neoneye/SwiftyFORM

ViewControllerFormItem and prepareForSegue

hatembr opened this issue ยท 6 comments

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

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?

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
9928278

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.

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

Thank you @hatembr

you're welcome ๐Ÿ‘

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