Ofri
Ofri

Reputation: 289

Segue and Unwind segue without UI element

I have been looking for an answer for this on SO without success.

I have UIViewController A and B. They are NOT linked in Storyboard, and I want to perform a Segue from A to B, and upon clicking a button in B, to activate an unwindSegue to A.

So far I have :

1) The @IBAction in A to unwind back to 2) The function to prepare the Segue from A to B (In A) 3) The button to call the unwindSegue from B to A

What I'm missing is the logic of the function in 2.

More info about B:

Title: FilterView Class: FilterViewController Storyboard ID: FilterView

I tried creating a segue:

let segueToFilter = UIStoryboardSegue(identifier: "SegueToFilterView", source: self, destination: FilterViewController)

Thinking this might just get me what I need.

but I get this error:

Cannot convert value of type 'FilterViewController.Type' to expected argument type UIViewController

Help would be appreciated :)

Upvotes: 1

Views: 72

Answers (1)

Sweeper
Sweeper

Reputation: 270770

I still do not understand how segues are causing problems to you, but if you really really don't want to use segues, you can try presenting and dismissing VCs programmatically.

The idea goes like this:

  • Create an instance of your VC
  • Pass data to it by setting some properties
  • Present!
  • In the VC, dismiss.

The actual code will be different if you embedded your VCs in a navigation controller. Instead of calling present and dismiss, you would call pushVC and popVC on the navigation controller.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "FilterView") as FilterViewController
vc.someProperty = someData // this is where you pass data
vc.present(vc, animated: true, completion: nil)

To pass data back, you should use the delegate pattern by creating a FilterViewDelegate that the first VC conforms to. Look up on Google for more info.

Upvotes: 1

Related Questions