使用Segue自动加载SecondViewController

时间:2018-09-23 20:37:19

标签: xcode macos cocoa swift4 segue

如何在SecondViewController方法内与ViewController同时加载viewDidLoad()

Segue的类型为Sheet

import Cocoa

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.performSegue(withIdentifier: "secondVC", sender: self)
    }

    func prepareforSegue(segue: NSStoryboardSegue, sender: Any?) {
        if let identifier = segue.identifier, identifier == "secondVC" {
            _ = segue.destinationController as? SecondViewController
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:2)

self.performSegue(withIdentifier: "secondVC", sender: self)

viewDidLoad内部不起作用,请考虑将其包装在viewDidAppear中的Dispatch-after或bool标志内部

DispatchQueue.main.asyncAfter(deadline:.now() + 2 ) {
   self.performSegue(withIdentifier: "secondVC", sender: self)
}

OR

var once = true
override func viewDidAppear() {
   super.viewDidAppear()
   if once {
        self.performSegue(withIdentifier: "secondVC", sender: self)
        once = false
   }
}