本文介绍了Xcode Swift 3:定时器和 Segue 视图控制器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在 ViewController 1 中.goToMainUI 被分配给 ViewController 1 和 2 之间的 segue 连接 ID.此外,ViewController 2 的故事板 ID 是相同的 (goToMainUI).定时器结束后报错,ViewControllers不切换.有谁知道问题是什么?谢谢!

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

推荐答案

试试这个代码:

注意:在 Swift 3 中测试的代码.

Note: Code tested in Swift 3.

第一步:首先设置storyboard Segue Identifier

Step 1: First set storyboard Segue Identifier

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

    }

}

在您的 ViewTwo 中添加上面的 viewDidLoad 方法.

In your ViewTwo add this above viewDidLoad method.

  var emptyString = String()

这篇关于Xcode Swift 3:定时器和 Segue 视图控制器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 11:04