2015-09-26 67 views
0

我想在定時器完成後執行一個segue,但是我無法得到segue執行。Swift NSTimer and shouldPerformSegueWndIdentifier issue

這裏是我的代碼

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 
} 

我是新來的斯威夫特所以任何幫助,將不勝感激。

謝謝!

+0

你的問題是什麼? –

+0

一旦計時器運行,shouldPeformSegueWithIdentifier不會執行 – user3080228

+0

您能詳細說明您想要達到的目標嗎? –

回答

0

你可以使用GCD來延緩你的延續。以下是您的需求示例代碼:

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項目瞭解更多信息。

+0

我會給那個走。謝謝! – user3080228

+0

當然樂意幫助你.. :) –