Swift - 使用IBOutlet&在功能中的IBAction?

时间:2015-03-31 07:23:13

标签: swift

我在柜台上工作。我想在60秒过后停止他的工作。为此,我使用此代码:

class FirstViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    //calling the wait function
    self.callForWait()    
}

func game(){        
    var score : Int = 0

    @IBOutlet weak var afficheurScore: UILabel!

    @IBAction func boutonPlus(sender: UIButton) {

        score = score + 1

        afficheurScore.text = "\(score)"   
    }
}

func callForWait(){
    //setting the delay time 60secs.
    let delay = 60 * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue()) {
        //call the method which have the steps after delay.
        self.stepsAfterDelay()
    }
}

func stepsAfterDelay(){
    //your code after delay takes place here...
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

boutonPlus是一个按钮,当我点击afficheurScore时,一个简单的标签说(数字+ 1)。

在我的游戏功能中,我遇到了这个错误:

  

"只有实例属性可以声明为IBOutlet / IBAction"

1 个答案:

答案 0 :(得分:1)

将此代码移出game()函数

    @IBOutlet weak var afficheurScore: UILabel!

    @IBAction func boutonPlus(sender: UIButton) {
        score = score + 1
        afficheurScore.text = "\(score)"

    }

所以你在课堂上有它,现在他们定义里面 game()函数

完整的代码应该是:

class FirstViewController: UIViewController {

  var score : Int = 0

  @IBOutlet weak var afficheurScore: UILabel!

  @IBAction func boutonPlus(sender: UIButton) {
        score = score + 1
        afficheurScore.text = "\(score)"
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    //calling the wait function
    self.callForWait()
  }

  func game(){
  }

  func callForWait(){
    //setting the delay time 60secs.
    let delay = 60 * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue()) {
        //call the method which have the steps after delay.
        self.stepsAfterDelay()
    }
  }


  func stepsAfterDelay(){
    //your code after delay takes place here...
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
}
相关问题