-1

I am new to iOS development. I am developing an application using iOS Swift. I have 3 view controllers viewController1, viewController2, viewController3. When I click from viewController1 its move to viewController2. By using the following code

let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let secondViewController = storyboard.instantiateViewControllerWithIdentifier("viewController2")
        presentViewController(secondViewController, animated: false, completion: nil)

Now I am moving from viewController2 to viewController3 by using the following code

performSegueWithIdentifier("viewController3", sender: scoreColorArray);

It's all fine. Now I want to back from viewController3 to viewController1. My problem it is not going to viewController1 it is going to viewController2. I know the reason viewController2 not dismissed.

I want to dismiss the current view controller and start new one. How can I do it? I know there is some ways to do it, please someone help me to finds the issue.

Amsheer
  • 7,046
  • 8
  • 47
  • 81
  • call dismissViewController on the one you presented – Daij-Djan Dec 10 '15 at 09:16
  • "I want to dismiss the second one and go to third one". When I call dismiss from second it is not going to third view controller. – Amsheer Dec 10 '15 at 09:20
  • 1
    Your comment is a bit confusing. You want to go from VC3 to VC1, is that right? If VC1 presents VC2 and VC2 presents VC3 then calling `self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated` on VC3 should take you to VC1. – spassas Dec 10 '15 at 09:30
  • @spassas Exactly this is what I wanted. Could you please add this in answer. It is working. – Amsheer Dec 10 '15 at 09:37

2 Answers2

2

Since your presentation sequence is VC1 -> VC2 -> VC3 you need to call self.presentingViewController?.presentingViewController?.dismissViewControllerA‌​animated on VC3, traversing this way your sequence in reverse.

spassas
  • 4,778
  • 2
  • 31
  • 39
0

You can achieve it by using delegate.

  • You send delegate call from controller 2 to controller 1 for dismiss controller 2.
  • After dismiss success, you will push controller 3 from controller 1

You can look my demo for this case: Demo Pushview

vien vu
  • 4,277
  • 2
  • 17
  • 30