1

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 :)

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Ofri
  • 289
  • 5
  • 17
  • What stops you from adding a segue in the storyboard? – Sweeper Sep 15 '18 at 07:32
  • I have several UIViewControllers that can segue to it and I do not want to add like 3-4 visual segues – Ofri Sep 15 '18 at 07:44
  • Then you should just call the `present` and `dismiss` methods to do this, without segues. – Sweeper Sep 15 '18 at 07:44
  • So how do I activate a method after dismiss ? And how do I transfer info to the screen to show up after dismiss ? – Ofri Sep 15 '18 at 07:45
  • _"I do not want to add like 3-4 visual segues"_ why not? – Ashley Mills Sep 15 '18 at 07:46
  • @Sweeper Actually it runs deeper than this, I CAN add a visual segue but I will need to remove 2 of the 3 ViewControllers I have which might be a headache as this time – Ofri Sep 15 '18 at 07:47
  • @AshleyMills I want to keep it as clean as possible... Actually there might be something I might be able to do but I will need guidance in this (Removing 2 of the 3 views which inherit from the same view) – Ofri Sep 15 '18 at 07:48
  • Its not clear what you mean by _"keep it as clean as possible"_. How does adding a segue in the storyboard make it "unclean". If it's a problem with unwind methods in a superclass, how about adding the unwind Action to your subclasses rather than your superclass? – Ashley Mills Sep 15 '18 at 07:52
  • 1
    @AshleyMills I think I will address the bigger picture here and try to remove s creens. I will edit question soon – Ofri Sep 15 '18 at 07:56

1 Answers1

1

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.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 1
    Thank you :) You also made me realize I should address the bigger picture here, and so I will soon post a new question – Ofri Sep 15 '18 at 08:05