类型'UIViewController'没有成员updateTimer

时间:2019-03-26 00:47:34

标签: swift

我正在尝试访问以下设备中的timer属性 UIViewController

对于最新版本的swift UIViewController似乎没有成员“ updateTimer”

class InGameViewController: UIViewController {

    @IBOutlet weak var countdownTimerLabel: UILabel!
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var pointsLabel: UILabel!

    @IBOutlet weak var initialCountdownLabel: UILabel!

    var seconds = 5 
    var timer = Timer()
    var isTimerRunning = false 

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let value = UIInterfaceOrientation.landscapeLeft.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")

         }

    func runTimer() {

        timer = Timer.scheduledTimer(timeInterval: 1, target: self,   selector: (#selector(UIViewController.updateTimer)), userInfo: nil, repeats: true)
    }
}

3 个答案:

答案 0 :(得分:0)

您需要首先创建一个updateTimer函数,以便在ScheduledTimer函数的选择器函数中使用。 确保将其装饰为objc。

还请删除选择器函数中的UIViewController,因为该函数仅存在于您当前的viewcontroller类(InGameViewController)中,而不存在于父UIViewController类中。

    @objc func updateTimer() {
             print("timer updated")
    }

    timer = Timer.scheduledTimer(timeInterval: 1, target: self,   selector: (#selector(updateTimer)), userInfo: nil, repeats: true)

答案 1 :(得分:0)

UIViewController没有名为updateTimer

的属性

我认为您不能只在#selector()中调用属性,因为它的签名错误,并且会失败。

作为参考,您正在调用的API参考: https://developer.apple.com/documentation/foundation/timer/1412416-scheduledtimer

答案 2 :(得分:0)

class InGameViewController: UIViewController {

    @IBOutlet weak var countdownTimerLabel: UILabel!
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var pointsLabel: UILabel!

    @IBOutlet weak var initialCountdownLabel: UILabel!

    var seconds = 5 
    var timer = Timer()
    var isTimerRunning = false 

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let value = UIInterfaceOrientation.landscapeLeft.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")

         }

    func runTimer() {

        timer = Timer.scheduledTimer(timeInterval: 1, target: self,   selector: (#selector(self.updateTimer)), userInfo: nil, repeats: true)
    }
}

@objc func updateTimer() {
             print("timer updated")
    }