【发布时间】:2017-02-08 04:49:20
【问题描述】:
我有一个管理AVPlayer 和ViewController 的类,显示AVPlayer。在ViewController 我有 3 个IBOutlets:
@IBOutlet weak var playButton: UIButton!
@IBOutlet weak var progressBar: UIProgressView!
@IBOutlet weak var timerLabel: UILabel!
问题是我不知道如何将IBOutlets 从ViewController 传递给我的玩家管理类。我总是得到nil 和
致命错误:在展开可选值时意外发现 nil
初始化progressBar、timerLabel或playButton时。
这是我的播放器管理类中的一些代码:
import UIKit
import AVFoundation
class RecordsAudioPlayer: NSObject
{
var player = AVPlayer()
var timerUpdateAudioProgressView = Timer()
var timerUpdateTime = Timer()
var isPlaying: Bool = false
...
func updateAudioProgressView()
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "callRecords") as! CallRecordsViewController
let progressBar = vc.progressBar!
print("method fired: updateAudioProgressView ", NSDate.init(timeIntervalSinceNow: 0).description)
if isPlaying
{
let currentPlayerItem = player.currentItem
let duration = Float((currentPlayerItem?.asset.duration.value)!)/Float((currentPlayerItem?.asset.duration.timescale)!)
let currentTime = Float(player.currentTime().value)/Float(player.currentTime().timescale)
progressBar.setProgress(Float(currentTime/duration), animated: false)
}
}
func updateTime()
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "callRecords") as! CallRecordsViewController
let timerLabel: UILabel! = vc.timerLabel! //***I get exception here
print("method fired: updateTime ", NSDate.init(timeIntervalSinceNow: 0).description)
let currentTime = Int64(player.currentTime().value)/Int64(player.currentTime().timescale)
let minutes = currentTime / 60
let seconds = currentTime - minutes * 60
timerLabel.text = String(format: "%02d : %02d", Int(minutes), Int(seconds))
}
【问题讨论】: