2016-09-26 71 views
0

我將其中一個變量設置爲nil,然後將其設置爲viewDidLoad後,我遇到了一個奇怪的問題。viewDidLoad中的變量集將變爲零

我正在使用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始終是零

我試圖去通過代碼和viewWillAppear中,customRecordButton是也沒有在那裏。我不知道是否這是我用從這裏使用的RecordButton類的問題:https://github.com/samuelbeek/RecordButton 我不認爲應該有問題..

編輯1:在viewDidLoad上面添加變量。我讓他們忘了粘貼它們。

編輯2:順便說一句,錄音按鈕顯示在我的手機用戶界面,只是當我按下它,它告訴我customRecordButton是零

+0

試圖聲明'讓customRecordButton = RecordButton()上面的''viewDidLoad中'然後在'viewDidLoad'set it'customRecordButton = RecordButton(frame:recordButtonRect)' –

+0

對不起,我應該在我原來的答案中加入這個。我確實在viewDidLoad上面設置了變量。我已經編輯了代碼 – makthrow

+0

在'viewdidload'上面你有'var customRecordButton'並且裏面有'let customRecordButton',你的代碼中是這樣嗎?如果是,那麼你有你的問題。 –

回答

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) 
+0

對不起,我應該在我原來的答案中加入這個。我確實在viewDidLoad上面設置了變量 – makthrow

+0

是的,你再次在本地範圍內的viewDidLoad中聲明'customRecordButton'。在那裏刪除那個'let'。 – RyuX51

+0

謝謝!我不知道我是怎麼錯過的。 – makthrow