简体   繁体   English

在 Swift 中添加 UIViewController

[英]Adding a UIViewController in Swift

I have created a new class in another file in Swift.我在 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.第二个类是一个 UIViewController,它将包含 mediaPickers。我想将此 viewController 添加到场景中,但我不知道如何添加。

I have tried我试过了

  MyViewController().viewDidLoad()

and

  MyViewController().viewDidLoad(YES)

However, both of these call the methods without adding the viewController to the scene.但是,这两个调用方法都没有将 viewController 添加到场景中。 Any ideas of how to load the viewController?关于如何加载 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.我已经快速创建了这个文件并将它添加到我的 SpriteBuilder 项目中。 The SpriteBuilder main scene is a CCNode. SpriteBuilder 主场景是一个 CCNode。 From here I want to loa a UIViewController so that I can add a add the mediaPicker to the view.从这里我想加载一个 UIViewController 以便我可以将 mediaPicker 添加到视图中。

对于对此问题的答案感兴趣的其他人,我所做的是获取 rootViewController 并为其添加 UIView。

 let presentedVC = UIApplication.sharedApplication().keyWindow?.rootViewController

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM