Phillip Phillip - 1 month ago 31
Swift Question

Xcode Swift 3: Timer and Segue View Controller Error

This code is in ViewController 1. goToMainUI is assigned to the segue connection ID between ViewController 1 and 2. Also, the storyboard ID for ViewController 2 is the same (goToMainUI). After the timer is finished, there is an error and the ViewControllers do not switch. Anyone know what the problem is? Thanks!

override func viewDidLoad() {
super.viewDidLoad()

let timer = Timer.scheduledTimerWithTimeInterval(8.0, target: self, selector: #selector(timeToMoveOn), userInfo: nil, repeats: false)

func timeToMoveOn() {
self.performSegue(withIdentifier: "goToMainUI", sender: self)
}

Joe Joe
Answer

Try this code:

Note: Code tested in Swift 3.

Step 1: First set storyboard Segue Identifier

enter image description here

Step 2:

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

    Timer.scheduledTimer(timeInterval: 8.0, target: self, selector: #selector(timeToMoveOn), userInfo: nil, repeats: false)

    }

    func timeToMoveOn() {
    self.performSegue(withIdentifier: "goToMainUI", sender: self)
   }

   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if (segue.identifier == "goToMainUI") {

        segue.destination as! viewTwo // viewTwo is your destination ViewController

        print("Segue Performed")

    }

}