makthrow
makthrow

Reputation: 139

variable set in viewDidLoad is becoming nil later

I run into a weird problem with one of my variables being set to nil after I set it in viewDidLoad.

I'm using a videoPreviewLayer AVPreview and there's a lot of different session queues going on so maybe I'm not understanding something properly. I could use help diagnosing the issue. The code is quite long but it is straightforward.. any help would be greatly appreciated. The full code is here: https://gist.github.com/makthrow/0570cc571f1c7866e094096626c19498

and the short version:

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()
    }

}

basically , in updateProgress(), customRecordButton is always nil

I tried going through the code and in viewWillAppear, customRecordButton is nil in there as well. I'm not sure if it's a problem with the RecordButton class i'm using from here :https://github.com/samuelbeek/RecordButton I don't think there should be a problem ..

edit 1: added variables above viewDidLoad. I had them there just forgot to paste them.

edit 2: btw, the record button shows up on my phone UI, just when I press it it tells me customRecordButton is nil

Upvotes: 0

Views: 238

Answers (1)

RyuX51
RyuX51

Reputation: 2877

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)

Upvotes: 2

Related Questions