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

标签 ios swift iboutlet

我有一个类用于管理 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/

相关文章:

swift - 将 UITableViewCell 的 IBOutlet 与两个不同的 UITableViewController 重用

iphone - UITabBarItem 执行 IBAction Xcode 4.2

swift - 如何在 SwiftUI 中使用 MVVM 持久化数据?

ios - firstRect(对于 :) returns wrong value in UITextField

xcode - 从另一个类更改 IBOutlet 的值

iphone - UIPageViewController,如何在不打乱数据源指定顺序的情况下正确跳转到特定页面?

ios - 如何从 cellForRowAtIndexPath 获取 textField 上的边框线?

ios - 如何使用自定义设计显示一串数字?

iphone - 如何使用 iPhone 计算我的速度?

ios - 防止在移动 Safari/Webview 中的 touchstart 上出现灰色覆盖