viewDidLoad中的变量集将在以后变为零

时间:2016-09-26 21:57:54

标签: swift

我遇到一个奇怪的问题,我在viewDidLoad中设置了一个变量后设置为nil。

我正在使用videoPreviewLayer AVPreview并且有很多不同的会话队列正在进行中,所以我可能没有正确理解某些内容。 我可以使用帮助来诊断问题。 代码很长但很简单..任何帮助将不胜感激。完整代码位于:https://gist.github.com/makthrow/0570cc571f1c7866e094096626c19498

和简短版本:

var customRecordButton : RecordButton!
var progressTimer : Timer!
var progress : CGFloat! = 0



override func viewDidLoad() {
    super.viewDidLoad()



    // custom recordButton https://github.com/samuelbeek/recordbutton
    let recordButtonRect = CGRect(x: 0, y: 0, width: 70, height: 70)
    let customRecordButton = RecordButton(frame: recordButtonRect)
    customRecordButton.center = self.view.center
    customRecordButton.progressColor = UIColor.red
    customRecordButton.closeWhenFinished = false
    customRecordButton.center.x = self.view.center.x
    customRecordButton.addTarget(self, action: #selector(self.record), for: .touchDown)
    customRecordButton.addTarget(self, action: #selector(self.stop), for: UIControlEvents.touchUpInside)
    self.view.addSubview(customRecordButton)
    }
func record() {
    self.progressTimer = Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
}

func updateProgress() {


    let maxDuration = CGFloat(5) // max duration of the recordButton

    progress = progress + (CGFloat(0.05) / maxDuration)
    customRecordButton.setProgress(progress) // (customRecordButton is always nil here)

    if progress >= 1 {
        progressTimer.invalidate()
    }

}

基本上,在updateProgress()中,customRecordButton始终为nil

我尝试浏览代码,在viewWillAppear中,customRecordButton也在那里。我不确定RecordButton类是否存在问题我在这里使用:https://github.com/samuelbeek/RecordButton 我不认为应该有问题。

编辑1:在viewDidLoad上方添加变量。我把它们放在那里只是忘了粘贴它们。

编辑2:顺便说一句,记录按钮显示在我的手机用户界面上,就在我按下它时告诉我customRecordButton是零

1 个答案:

答案 0 :(得分:2)

var customRecordButton: RecordButton?

override func viewDidLoad() {
    super.viewDidLoad()

    // custom recordButton https://github.com/samuelbeek/recordbutton
    let recordButtonRect = CGRect(x: 0, y: 0, width: 70, height: 70)
    customRecordButton = RecordButton(frame: recordButtonRect)

...

customRecordButton?.setProgress(progress)