简体   繁体   English

接收不同类的委托方法

[英]Receive delegate method in different class

I'm developing a music player by using Jukebox library. 我正在使用Jukebox库开发音乐播放器。 It has a delegation which warn elements when songs duration/ current time etc. changed. 它具有一个委派,可以在歌曲持续时间/当前时间等发生变化时警告元素。 In Jukebox example, all of codes are in ViewController. 在自动存储塔示例中,所有代码都在ViewController中。 Therefore, for example when current time changes, it also changes sliders value. 因此,例如,当当前时间更改时,它也会更改滑块值。 I want to create a player class which is usable by more than one ViewController. 我想创建一个供多个ViewController使用的播放器类。 When I put delegate methods inside this model class. 当我将委托方法放入此模型类中时。 How can I reach them in ViewController classes? 如何在ViewController类中找到它们?

First Screenshot is first ViewController which has delegates methods and initialize player. 第一个屏幕截图是第一个ViewController,它具有委托方法和初始化播放器。

MainViewController I need to reach that delegate methods in secondViewController to update the UI. MainViewController我需要在secondViewController中达到该委托方法来更新UI。

class ViewController: UIViewController, JukeboxDelegate {

var jukebox : Jukebox!

override func viewDidLoad() {
    super.viewDidLoad()
    configureUI()

    // begin receiving remote events
    UIApplication.shared.beginReceivingRemoteControlEvents()

    // configure jukebox
    jukebox = Jukebox(delegate: self, items: [
        JukeboxItem(URL: URL(string: "http://www.kissfm.ro/listen.pls")!),
        JukeboxItem(URL: URL(string: "http://www.noiseaddicts.com/samples_1w72b820/2514.mp3")!),
        JukeboxItem(URL: URL(string: "http://www.noiseaddicts.com/samples_1w72b820/2958.mp3")!)
        ])!
}
// MARK:- JukeboxDelegate -

func jukeboxDidLoadItem(_ jukebox: Jukebox, item: JukeboxItem) {
    print("Jukebox did load: \(item.URL.lastPathComponent)")
}

func jukeboxPlaybackProgressDidChange(_ jukebox: Jukebox) {

    if let currentTime = jukebox.currentItem?.currentTime, let duration = jukebox.currentItem?.meta.duration {
        let value = Float(currentTime / duration)
        slider.value = value
        populateLabelWithTime(currentTimeLabel, time: currentTime)
        populateLabelWithTime(durationLabel, time: duration)
    } else {
        resetUI()
    }
}

func jukeboxStateDidChange(_ jukebox: Jukebox) {

    UIView.animate(withDuration: 0.3, animations: { () -> Void in
        self.indicator.alpha = jukebox.state == .loading ? 1 : 0
        self.playPauseButton.alpha = jukebox.state == .loading ? 0 : 1
        self.playPauseButton.isEnabled = jukebox.state == .loading ? false : true
    })

    if jukebox.state == .ready {
        playPauseButton.setImage(UIImage(named: "playBtn"), for: UIControlState())
    } else if jukebox.state == .loading  {
        playPauseButton.setImage(UIImage(named: "pauseBtn"), for: UIControlState())
    } else {
        volumeSlider.value = jukebox.volume
        let imageName: String
        switch jukebox.state {
        case .playing, .loading:
            imageName = "pauseBtn"
        case .paused, .failed, .ready:
            imageName = "playBtn"
        }
        playPauseButton.setImage(UIImage(named: imageName), for: UIControlState())
    }

    print("Jukebox state changed to \(jukebox.state)")
}

func jukeboxDidUpdateMetadata(_ jukebox: Jukebox, forItem: JukeboxItem) {
    print("Item updated:\n\(forItem)")
}

If you want to use music playerview from anywhere 如果您想从任何地方使用音乐播放器视图

  1. you should write player class all code for playing files should be in this and just need to pass array of list from anywhere and it can played.and use custom delegation for appropriate response in playerview 您应该编写播放器类,用于播放文件的所有代码都应该在此文件中,并且只需要从任何地方传递列表数组就可以播放。并使用自定义委托在播放器视图中进行适当的响应

  2. for UI make a viewcontroller and u can show it from any class so you can call from anywhere in the app 为UI创建一个viewcontroller,您可以从任何类中显示它,因此您可以从应用程序中的任何位置调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一个类中实现具有不同委托和数据源方法的两个Tableview? - HOW Implement Two Tableview with different delegate and datasource method In One Class? 在类方法中使用委托 - using a delegate in a class method 从主线程上的委托方法接收答案 - Receive answer from a delegate method on main thread 在自定义类中未调用委托方法 - Delegate method is not called in a custom class 如何在ViewController中获取委托方法以查看*记录器何时完成(不同类) - How can I get a delegate method in ViewController to see when *recorder is finished (different class) 从多个视图控制器接收委托方法调用 - Receive delegate method calls from multiple view controllers UIwebView的shouldStartLoadWithRequest:委托方法不接收所有加载请求 - UIwebView's shouldStartLoadWithRequest: delegate method Does not Receive all Load Requests 如何使用闭包从 class 接收异步数据,SwiftUI 中有委托? - How to use closure to receive async data from a class with delegate in SwiftUI? 如何在同一个委托类中调用Delegate方法作为选择器 - How to call Delegate method as a selector in same delegate class 使用相同的选择器的不同委托方法? - Using to different delegate method for the same picker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM