virgil debique virgil debique - 4 years ago 179
Swift Question

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




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


import Foundation
import UIKit
import MediaPlayer

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

override func viewDidLoad()


override func viewDidAppear(animated: Bool)

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..."

presentViewController(picker, animated: true, completion: nil)
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.

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download