gpt4 book ai didi

ios - 如何在两个类之间传递 IBOutlet?

转载 作者:行者123 更新时间:2023-11-28 12:30:56 25 4
gpt4 key购买 nike

我有一个类用于管理 AVPlayerViewController 显示 AVPlayer。在 ViewController 中,我有 3 个 IBOutlets:

@IBOutlet weak var playButton: UIButton!
@IBOutlet weak var progressBar: UIProgressView!
@IBOutlet weak var timerLabel: UILabel!

问题是我不知道如何将 IBOutletsViewController 传递到我的播放器管理类。我总是得到 nil

fatal error: unexpectedly found nil while unwrapping an Optional value

当初始化 progressBartimerLabelplayButton 时。

这是我的播放器管理类的一些代码:

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

最佳答案

在这种情况下,代表们来拯救我们。为您的 RecordsAudioPlayer 类创建委托(delegate)方法。并使用它们来更新进度条和计时器标签。

protocol RecordsAudioPlayerDelegate
{
func audioplayerProgressUpdated(time:CGFloat) // change the method as your wish
}

class RecordsAudioPlayer: NSObject
{
var delegate:RecordsAudioPlayerDelegate?
var player = AVPlayer()
var timerUpdateAudioProgressView = Timer()
var timerUpdateTime = Timer()
var isPlaying: Bool = false
...
...
...

//Call the delegate method from where you get the player current time
self.delegate.audioplayerProgressUpdated(playerTime)

//For controlling play/pause using play button
func playButtonClicked()
{
//play/pause player based on player status
}
}

在创建 RecordsAudioPlayer 对象时/之后,在您的 ViewController 中将委托(delegate)设置为 self

class YourViewController : UIViewController, RecordsAudioPlayerDelegate
{
var recordsAudioPlayer : RecordsAudioPlayer!

// set delegate where you create object for RecordsAudioPlayer
self.recordsAudioPlayer.delegate = self

//Now implement the delegate function
func audioplayerProgressUpdated(time:CGFloat)
{
//update progress view and timer label
}

//And inside from the play button action method
@IBAction func playButtonClicked(sender: UIButton)
{
self.recordsAudioPlayer.playButtonClicked()
}

}

关于ios - 如何在两个类之间传递 IBOutlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42104946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com