Phillip
Phillip

Reputation: 193

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)
    }

Upvotes: 0

Views: 967

Answers (1)

Joe
Joe

Reputation: 8986

Try this code:

Note: Code tested in Swift 3.

Step 1: First set storyboard Segue Identifier

enter image description here

Step 2:

   let emptyString = String() // Do nothing 

   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") {

    let dest = segue.destination as! viewTwo // viewTwo is your destination ViewController
    dest.emptyString = emptyString

        print("Segue Performed")

    }

}

In your ViewTwo add this above viewDidLoad method.

  var emptyString = String()

Upvotes: 1

Related Questions