-1

Here is the first viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Tutorial"]) {
        [self performSegueWithIdentifier:@"fromTutorialToWelcome" sender:self];
    }else{
        [self setupVc];
    }
}

And here is the second, in the following view controller:

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"loginTesting"]) { //This evaluates to true, I double and triple checked.

        [self performSegueWithIdentifier:@"fromWelcomeToEntryPoint" sender:self];
    }else{
        [self setupVc];
    }
}

Both segues are Show segues, so why doesn't the second one work?

EDIT: Stressing the type of segue, since this is no longer iOS5.

EDIT2: I guess I didn't properly explain what I want to do. I wish to have the third view controller displayed without seeing the second view controller.

Jargen89
  • 480
  • 1
  • 6
  • 19
  • 1
    What happens? What messages do you get? Have you set a breakpoint or added logging to `prepareForSegue`? – Paulw11 Oct 08 '15 at 20:16
  • Nothing happens, the app doesn't move anywhere. I just overridden PrepareForSegue, but all it has is an NSLog – Jargen89 Oct 08 '15 at 20:26
  • I do not think performing segues in the viewDidLoad is a good idea. It is just too early -> http://stackoverflow.com/questions/8221787/perform-segue-on-viewdidload – luk2302 Oct 08 '15 at 21:11

1 Answers1

2

Each view is loaded into memory only once when you open your app from non running state. In the second view controller move it from viewDidLoad to viewDidAppear. I believe it will solve your problem. Let me know if it didn't and I'll try to debug it.

EDIT:

Here is a working example I made.

First time: FirstViewController -> SecondViewController -> ThirdViewController

Second time: FirstViewController -> ThirdViewController (without seeing the SecondViewController!) storyboard first

FirstViewController.swift:

import UIKit

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func ToSecondPressed(sender: AnyObject) {

        if let skip = NSUserDefaults.standardUserDefaults().valueForKey("SkipSecond") as? Bool{

            let Third: ThirdViewController = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("Third") as! ThirdViewController

            UIApplication.sharedApplication().delegate?.window??.rootViewController = Third
        }

        else{

            let Second: SecondViewController = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("Second") as! SecondViewController

            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "SkipSecond")
            UIApplication.sharedApplication().delegate?.window??.rootViewController = Second
        }

    }

}

SecondViewController.swift:

import UIKit

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func ToThirdPressed(sender: AnyObject) {
        let Third = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("Third") as? ThirdViewController

        UIApplication.sharedApplication().delegate?.window??.rootViewController = Third
    }


}

ThirdViewController.swift:

import UIKit

class ThirdViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
Yonatan Vainer
  • 453
  • 5
  • 15
  • While viewDidLoad is only ever triggered once, this shouldn't be an issue as the second ViewController isn't instantiated until the first segue starts. And also, while viewDidAppear does solve the the navigation, I end up seeing the second viewcontroller, which I want to avoid and skip to the third viewController I'm trying to reach – Jargen89 Oct 09 '15 at 12:25
  • @Jargen89 I added working example to my answer. The main key here is to manipulate the window of the app and assign it according to the viewController you want (Second or Third). I don't like segues and prefer using the instantiateViewController method of the storyboard... – Yonatan Vainer Oct 09 '15 at 15:44