0

I'm presenting a tutorial of 4 UIViewController when my app starts the first time. Every UIViewController has a Button with a segue presenting the next ViewController. The last ViewController has a button "Let's start" which should dismiss the tutorial completely.

Problem: It this dismiss all ViewControllers except the first. I don't understand why?!

What I expect:

On the last ViewController4 I'm calling the dismissIntroduction() function of the first ViewController, so I except ALL ViewControllers (ViewController1 included) should disappear. When I put a button on the first ViewController and call the function "dismissIntroduction()" it disappears.

ViewController 1 (WelcomeViewController):

protocol WelcomeViewDelegate {
    func dismissIntroduction()
}

class WelcomeViewController: UIViewController, WelcomeViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    func dismissIntroduction() {
        self.dismissViewControllerAnimated(true, completion: nil)
    }


    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let destination = segue.destinationViewController as! ViewController2
        destination.delegate = self
    }

}

ViewController 2:

class ViewController2: UIViewController {

    var delegate:WelcomeViewDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
    }        

    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let destination = segue.destinationViewController as! ViewController3
        destination.delegate = self.delegate
    }

}

ViewController 3:

class ViewController3: UIViewController {

    var delegate:WelcomeViewDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
    }        

    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let destination = segue.destinationViewController as! ViewController4
        destination.delegate = self.delegate
    }

}

ViewController4 (the last one):

class ViewController4: UIViewController {

    var delegate:WelcomeViewDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func pressLetsStart(sender: AnyObject) {
        self.delegate!.dismissIntroduction()
    }

}

EDIT: I got it working, when I put the dismissViewControllerAnimated function TWO times!?

func dismissIntroduction() {
    self.dismissViewControllerAnimated(true, completion: nil)
    self.dismissViewControllerAnimated(true, completion: nil)
}

But why? I don't understand the logic behind...

mahega
  • 3,231
  • 4
  • 20
  • 32
  • did you try having this all four controllers in navigation controller and present NavController from let say PPViewContoller ? – mkumar Mar 02 '16 at 14:21
  • I don't want to put it in a navigation controller (because of the navigation bar). – mahega Mar 02 '16 at 15:00
  • 1
    you can always hide the navigation bar like in http://stackoverflow.com/questions/2926914/navigation-bar-show-hide, doing by apple way(navigation controller) is a better approach than running into many bugs or maintenance mare in long run.. – mkumar Mar 03 '16 at 05:45
  • Thanks mkumar, I'll try this. That sounds really like a clean solution – mahega Mar 03 '16 at 09:05

2 Answers2

0

You are looking at the wrong solution to your problem, what you need to do is look for Unwind Segues. Go through this tutorial: https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/

Sanjan Piya
  • 247
  • 2
  • 8
0

I solved the problem now with following function in last ViewController:

@IBAction func pressLetsStart(sender: AnyObject) {
    self.dismissModalStack()
}

private func dismissModalStack() {
    var vc = self.presentingViewController! as UIViewController
    while (vc.presentingViewController != nil) {
        vc = vc.presentingViewController!;
    }
    vc.dismissViewControllerAnimated(true, completion: nil)
}
mahega
  • 3,231
  • 4
  • 20
  • 32