2015-02-23 24 views
0

我想打一個按鈕,使紅色和綠色的背景開關,但我得到這個錯誤:預期的功能缺失return返回迅速

missing return in a function expected to return UIView

我很新的編碼,我已經搜索了一下,但我沒有找到任何東西。

語言:雨燕

我的代碼:

var timeLeft = 0 
import UIKit 

class ViewController: UIViewController { 

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

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

    var buttonColor: UIView! { 

     timeLeft = 1 
    } 

    func update() { 
     if timeLeft > 0 { 
      view.backgroundColor = UIColor.greenColor() 
     } 
     else { 

      view.backgroundColor = UIColor.redColor() 
     } 
     timeLeft = timeLeft - 1 

    } 
} 

感謝幫助

+1

看到這裏的問題:http://stackoverflow.com/questions/24689469/swift-error-missing-return-in-function – 2015-02-23 13:47:58

回答

2

您聲明buttonColor作爲在它的身體需要你返回UIView實例(因爲它的類型計算的屬性是UIView)。

herehere有關計算性能,並確保它符合您的需要,張貼在你的情況

0

您好,歡迎計算器

記得讓你的UIButton的連接到代碼的動作。否則,當你點擊按鈕時,你的代碼將不會運行。另外,考慮使用Bool(true/false)變量來跟蹤背景顏色。

試試下面的代碼(記得要通過故事板「click'功能連接到那個按鈕):

import UIKit 

class ViewController: UIViewController { 

    var isGreen = true 

    @IBAction func click() { 
     if isGreen { 
      view.backgroundColor = UIColor.redColor() 
      isGreen = false 
     } else { 
      view.backgroundColor = UIColor.greenColor() 
      isGreen = true 
     } 
    } 
} 
0

VAR buttonColor:UIView的! { 的get(){ 回報(的UIView對象) }}