performseguewithidentifier在一个viewDidLoad中工作但在另一个viewDidLoad中没有,使用" Show"塞格斯

时间:2015-10-08 20:11:24

标签: ios objective-c iphone navigation viewdidload

这是第一个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];
    }
}

这是第二个,在以下视图控制器中:

- (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];
    }
}

两个segues都是显示 segues,那么为什么第二个不工作呢?

编辑:强调segue的类型,因为这不再是iOS5。

EDIT2:我想我没有正确解释我想做什么。我希望在不看第二个视图控制器的情况下显示第三个视图控制器。

1 个答案:

答案 0 :(得分:2)

当您从非运行状态打开应用程序时,每个视图仅加载到内存中一次。在第二个视图控制器中,将它从viewDidLoad移动到viewDidAppear。我相信它会解决你的问题。如果它没有,请告诉我,我会尝试调试它。

编辑:

这是我做的一个工作示例。

第一次: FirstViewController - > SecondViewController - > ThirdViewController

第二次: FirstViewController - > ThirdViewController(没有看到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.
    }


}