9

Eureka は、さまざまなデータ型を含む新しいレコードの作成に使用されるため、MediaPickerその viewController をどのように提示するのか疑問に思っています。

どの行が仕事をしますか? PushRowまたButtonRow

これが私がこれを作成しようとする私のクラスです。

import UIKit
import MediaPlayer

import Eureka

public final class MusicRow<T: Equatable> : SelectorRow<T, PushSelectorCell<T>, SelectorViewController<T>>, RowType {

    public required init(tag: String?) {
        super.init(tag: tag)
        presentationMode = .Show(controllerProvider: ControllerProvider.Callback {
            return AddMusicViewController(){ _ in }
            }, completionCallback: { vc in
                vc.navigationController?.popViewControllerAnimated(true)
        })
    }
}

public class AddMusicViewController: MPMediaPickerController, MPMediaPickerControllerDelegate {

    var musicPicker: MPMediaPickerController!

    public var row: RowOf<MPMediaItemCollection>!

    public var completionCallback : ((UIViewController) -> ())?

    public override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        // Setup sections/rows for tableView
        addMusic()
    }

    // Initialize musicPicker and customize it
    func addMusic() {
        musicPicker = MPMediaPickerController.self(mediaTypes:.Music)
        musicPicker.delegate = self
        musicPicker.allowsPickingMultipleItems = true
        view.addSubview(musicPicker.view)
    }

    // After selection, store the data into an array
    public func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
        musicTemp = nil
        musicTemp = mediaItemCollection
        if musicTemp == nil {
            noMusic = true
        } else {
            noMusic = false
        }
        completionCallback?(self)
    }

    // Cancel mediaPickerController
    public func mediaPickerDidCancel(mediaPicker: MPMediaPickerController){
        // Dismiss the picker if the user canceled
        noMusic = true
        completionCallback?(self)
    }
}

以下は私が直面しているエラーです。

エラー

4

1 に答える 1

7

これは一般的な質問でしたが、私の要件はMPMediaPickerController.

以下は私のセットアップです。

import UIKit
import MediaPlayer

import Eureka

// MusicRow
public final class MusicRow : SelectorRow<MPMediaItemCollection, PushSelectorCell<MPMediaItemCollection>, AddMusicViewController>, RowType {
    public required init(tag: String?) {
        super.init(tag: tag)
        presentationMode = .Show(controllerProvider: ControllerProvider.Callback { return AddMusicViewController(){ _ in } }, completionCallback: { vc in vc.navigationController?.popViewControllerAnimated(true) })
displayValueFor = {
        guard var musicTitle = $0 else { return "" }
        musicTitle = musicTemp!
        let representativeItem = musicTitle.representativeItem
        print("representativeItem = \(representativeItem)")
        let representativeItemTitle = representativeItem?.title
        return  "\(representativeItemTitle)"
        }
    }
}

// MusicViewController
public class AddMusicViewController : UIViewController, TypedRowControllerType, MPMediaPickerControllerDelegate {

    public var row: RowOf<MPMediaItemCollection>!
    public var completionCallback : ((UIViewController) -> ())?

    lazy var musicPicker : MPMediaPickerController = { [unowned self] in
        let mediaPicker = MPMediaPickerController.self(mediaTypes:.Music)
        return mediaPicker
    }()

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nil, bundle: nil)
    }

    convenience public init(_ callback: (UIViewController) -> ()){
        self.init(nibName: nil, bundle: nil)
        completionCallback = callback
    }

    public override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(musicPicker.view)

        musicPicker.delegate = self
        musicPicker.allowsPickingMultipleItems = true

        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
    }

    // After selection, store the data into a temporary variable
    public func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
        musicTemp = nil
        musicTemp = mediaItemCollection
        if musicTemp == nil {
            noMusic = true
        } else {
            noMusic = false
            row.value? = musicTemp!
        }
        completionCallback?(self)
    }

    // Cancel mediaPickerController
    public func mediaPickerDidCancel(mediaPicker: MPMediaPickerController){
        // Dismiss the picker if the user canceled
        noMusic = true
        completionCallback?(self)
    }
}

個人的な要件に合わせて微調整することは難しくないと思います。

于 2016-09-13T04:13:44.113 に答える