-2

I have created a new class in another file in Swift. The second class is a UIViewController that will have mediaPickers in. I want to add this viewController to scene, but I can't figure out how.

I have tried

  MyViewController().viewDidLoad()

and

  MyViewController().viewDidLoad(YES)

However, both of these call the methods without adding the viewController to the scene. Any ideas of how to load the viewController?

Thanks

import Foundation
import UIKit
import MediaPlayer

class mediaViewController: UIViewController, MPMediaPickerControllerDelegate, AVAudioPlayerDelegate
{
   var musicPlayer : MPMusicPlayerController?
   var mediaPicker : MPMediaPickerController?
   var subViewLoad : UIView?

   override func viewDidLoad()
  {
    super.viewDidLoad()

    subViewLoad?.inputView
}

override func viewDidAppear(animated: Bool)
{
    displayMediaPickerAndPlayItem()
}

func displayMediaPickerAndPlayItem()
{
    mediaPicker = MPMediaPickerController(mediaTypes: .AnyAudio)

    if let picker = mediaPicker
    {
        print("Successfully instantiated a media picker")
        picker.delegate = self
        picker.allowsPickingMultipleItems = true
        picker.showsCloudItems = true
        picker.prompt = "Pick a song please..."
        view.addSubview(picker.view)

        presentViewController(picker, animated: true, completion: nil)
    }
    else
    {
        print("Could not instantiate a media picker")
    }
}
}

I have created this file in swift and added it to my SpriteBuilder project. The SpriteBuilder main scene is a CCNode. From here I want to loa a UIViewController so that I can add a add the mediaPicker to the view.

virgil debique
  • 357
  • 2
  • 11
  • 4
    NEVER EVER EVER call `viewDidLoad` – luk2302 Dec 22 '15 at 21:17
  • A controller does not have mediaPickers in it, a view has. The controller has references to those. You need to create an instance of the view+controller, using storyboards, or loading it from a nib or anything, but certainly not the way you currently do it. – luk2302 Dec 22 '15 at 21:19
  • I have just tried calling the showMediaPicker directly and get a warning 'MusicPlayer[330:29023] Warning: Attempt to present on whose view is not in the window hierarchy!' – virgil debique Dec 22 '15 at 21:36
  • Could you be clearer with the question ? And maybe show some more source code ? Also we never call the viewDidLoad method. – Antoine Dec 22 '15 at 22:03

1 Answers1

0

For anyone else that was interested in the answer to this, what I done was to get the rootViewController and add a UIView to that.

 let presentedVC = UIApplication.sharedApplication().keyWindow?.rootViewController
virgil debique
  • 357
  • 2
  • 11