iosswiftuiviewcontrollerspritebuilder

Adding a UIViewController in Swift


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.


Solution

  • 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