Xcode Swift 3: Timer and Segue View Controller Error

懵懂的女人 提交于 2019-12-02 19:54:17

问题


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

回答1:


Try this code:

Note: Code tested in Swift 3.

Step 1: First set storyboard Segue Identifier

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


来源:https://stackoverflow.com/questions/40326516/xcode-swift-3-timer-and-segue-view-controller-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!