Swift NSTimer和shouldPerformSegueWithIdentifier问题

时间:2015-09-26 10:53:28

标签: xcode swift segue nstimer

我试图在计时器完成后执行一个segue,但我无法让segue执行。

这是我的代码

    var timer = NSTimer()

@IBOutlet weak var imageView: UIImageView!

func buttonControls() {
    self.imageView.image = UIImage(named: "image1.png")
    self.timer = NSTimer(timeInterval: 1.0, target: self, selector: "shouldPerformSegueWithIdentifier:", userInfo: nil, repeats: false)
}

@IBAction func loginButton(sender: AnyObject) {
    buttonControls()
}

@IBAction func signupButton(sender: AnyObject) {
    buttonControls()
}

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

    self.imageView.image = UIImage(named: "image2.png")
}

override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
    if identifier == "loginSegue" {
        performSegueWithIdentifier("loginSegue", sender: self)
    } else {
        performSegueWithIdentifier("signupSegue", sender: self)
    }

    return true
}

我是Swift的新手,所以任何帮助都会受到赞赏。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用GCD来延迟您的segue。以下是根据您的需要提供的示例代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC)))

    override func viewDidLoad() {
        super.viewDidLoad()
        self.imageView.image = UIImage(named: "image2.png")
    }

    @IBAction func loginButton(sender: AnyObject) {
        self.imageView.image = UIImage(named: "image2.png")
        dispatch_after(dispatchTime, dispatch_get_main_queue(), {
            self.performSegueWithIdentifier("loginSegue", sender: self)
        })
    }

    @IBAction func signupButton(sender: AnyObject) {

        self.imageView.image = UIImage(named: "image2.png")
        dispatch_after(dispatchTime, dispatch_get_main_queue(), {
            self.performSegueWithIdentifier("signupSegue", sender: self)
        })

    }
}

Sample项目获取更多信息。