1

I have three View Controllers and want to go back from the third to the first on button click. I do the following:

  1. Add the target to my button: doneButton.addTarget(self, action: #selector(ThirdViewController.doneButtonPressed(_:)), for: .touchUpInside)
  2. Create the action in the first controller: @IBAction func unwindFromNewSubscription(segue: UIStoryboardSegue) { }
  3. Create the unwind segue linking it to the action from #2 in the storyboard and set the "chageFromNewToMain" identifier
  4. Create the action for my button in the third controller: @IBAction func doneButtonPressed(_ sender:UIButton!) { print("DONE BUTTON PRESSED") performSegue(withIdentifier: "changeFromNewToMain", sender: self) }

And it works, BUT when I tap the button in V3 it instantly disappears and I see the animation: V2 slides down, so finally I can see the V1. But I want to animate the V3 to slide down instead of disappearing to see V1 after that. Any ideas how to fix that?

(V1, V2, V3 - View Controller 1, 2, 3)

Egor Iskrenkov
  • 433
  • 4
  • 15

1 Answers1

1

Try to use presentingViewController?.dismiss(animated: true, completion: nil) inside your @IBAction func doneButtonPressed(_ sender:UIButton!) action instead of calling performSegue(withIdentifier: "changeFromNewToMain", sender: self).

iyuna
  • 1,787
  • 20
  • 24
  • Should I replace something to my Controller's name? – Egor Iskrenkov Aug 09 '18 at 17:39
  • @MrBlazOn How do you stack these controllers? Could you also put the breakpoint inside if block and check whether it's getting called at all? – iyuna Aug 09 '18 at 17:40
  • Yes, it's called. I'm not sure I understood you about stacking, I created them in the storyboard and created classes for each :) – Egor Iskrenkov Aug 09 '18 at 17:46
  • @MrBlazOn By stacking I mean how you show V2 from V1 and then V3 from V2? Do you have other segues for that? If yes which kind of segues? – iyuna Aug 09 '18 at 17:47
  • Modal segue V1-V2 and Show Segue V2-V3. Can it cause this issue? Should they be the same? – Egor Iskrenkov Aug 09 '18 at 17:49
  • @MrBlazOn That's definitely a reason. Check out my updated answer – iyuna Aug 09 '18 at 18:01
  • Yes, 'presentingViewController?.dismiss(animated: true, completion: nil)' closes the V3 and I see the V2, but how can I go from the V3 to V1? That is the question – Egor Iskrenkov Aug 09 '18 at 18:05
  • @MrBlazOn You might have not modal segue V1-V2 then. What kind of animations do you have? Is it sliding from bottom to top V2 appearance and sliding from right to left V3 appearance? – iyuna Aug 09 '18 at 18:08
  • No, it slides up in V1-V2 and V2-V3 too, I think, that is because I don't have navigation bar I V2 and V3, so even Show segue has this animation... – Egor Iskrenkov Aug 09 '18 at 18:10
  • @MrBlazOn What if you try to do `presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)` then? – iyuna Aug 09 '18 at 18:12
  • The same problem: V3 disappears and V2 slides down. Is it a bug? Or should I set modal segues on both controllers? – Egor Iskrenkov Aug 09 '18 at 18:14
  • @MrBlazOn Ideally there shouldn't be such situations. If you're presenting view controllers one by one, you should dismiss them gradually as well. Have you considered putting V2 and V3 into navigation? – iyuna Aug 09 '18 at 18:16
  • No, I can't fit the navigation bar in V2 and V3 with my design :( I just tried all kinds of stock animations in Xcode and V3 disappears in each of them. I just don't know what to do :( Can I unwind V3 and do this with V2 just after V3? It will look better than disappearing... (I'm sorry, I took too much of your time) – Egor Iskrenkov Aug 09 '18 at 18:26
  • @MrBlazOn Navigation controller does not have to have the navigation bar. You can call `setNavigationBarHidden(true, animated: false)` inside your V2 `viewDidLoad` method and there will be no navigation bar. – iyuna Aug 09 '18 at 18:31
  • I think, the best way for me is to use presentingViewController?.dismiss(animated: true, completion: nil) and presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil) to kill controllers one by one. It looks good. P.S Segue from the storyboard creates its own NavigationBar in storyboard and I don't know, how to hide it. P.P.S Thank you so much, I spent too much of your time, I'm so sorry. But I've got what I wanted, so thank you again :) – Egor Iskrenkov Aug 09 '18 at 18:39
  • @MrBlazOn In storyboard there is an option for the view controller in attributes inspector on right panel to set simulated metrics. None option for Top bar is what you need. P.S. Please, do not worry about my time. I can stop answering any time I want :) – iyuna Aug 09 '18 at 18:42
  • Ok, I set None, but NavigationBar appears on V2 and V3 although it’s not shown on the storyboard. I think this is because I have the navigation bar on V1 and this kind of segue requires it on V2 and V3 – Egor Iskrenkov Aug 09 '18 at 18:50
  • @MrBlazOn Did you add `setNavigationBarHidden(true, animated: false)` to your V2 `viewDidLoad` method? – iyuna Aug 09 '18 at 20:30