【问题标题】:Does the AVPlayerViewController automatically come with a AVPlayer object?AVPlayerViewController 是否自动带有 AVPlayer 对象?
【发布时间】:2016-11-06 17:40:10
【问题描述】:

Swift 和视图控制器范例的新手。想知道 ViewControllers 是否会自动附带它所描述的对象,例如,avplayerviewcontroller 是否会自动附带一个准备好使用的 avplayer 对象,还是我们仍然需要创建一个 avplayer 对象?另一个例子可能是 uitableviewcontroller,它是自动附带一个 tableview 对象还是我们需要在创建一个子类 uitableviewcontroller 的可可触摸类之后创建它。

这是一些编译示例代码,我不一定要创建一个 avplayer 对象,但似乎该对象已经准备好通过一个名为“player”的属性来使用:

import UIKit
import AVKit
import AVFoundation

class MeetTheAuthorViewController: UIViewController {



    override func viewDidLoad() {
        super.viewDidLoad()
        //playBackgroundMusic("bensound-jazzyfrenchy-castlesbackground.mp3")
        // Do any additional setup after loading the view.


    }

    func btn_clicked(_ sender: UIBarButtonItem) {
        // Do something
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "videoSegue"
        {
            //set up the player
            let videoURL = Bundle.main.url(forResource: "The Atlanta Goat_ Part One-HD", withExtension: "mp4")
            let videoViewController = segue.destination as! AVPlayerViewController
            videoViewController.player = AVPlayer(url: videoURL!)
            videoViewController.player?.play()


        }
    }


}

【问题讨论】:

    标签: ios swift avplayer viewcontroller avplayerviewcontroller


    【解决方案1】:

    是的。 XYFeatureViewControllers 是方便的UIViewController 子类。例如UITableViewController 继承自UIViewController,假定其viewUITableView 类型并且已经符合UITableViewDelegateUITableViewDataSource 协议。

    它们通常为委托方法提供默认实现,因此可以立即“正常工作”。

    如果是UITableView,我建议您使用自定义UIViewController 并自己添加UITableView(s) 以获得充分的灵活性。

    例如,UITableViewControllerUITableView 作为根视图,因此如果您希望稍后在屏幕上缩小 tableView(示例),则不能。

    在您的自定义 viewController 中,tableView 将是 rootView 的子视图,因此调整它的大小或在其旁边添加第二个不会有问题。

    【讨论】:

    • 所以您是说它们自动附带预定对象,例如 tableview 已经有一个 tableview 对象可以通过属性使用,而无需创建一个。在这种情况下,如果我们想在 uitableviewcontroller 中有两个 tableview 对象,我们必须为第二个实例化一个新的 tableview 对象? @shallowThought
    • “所以你是说它们会自动附带预定对象,例如 tableview 已经有一个 tableview 对象可以通过属性使用而无需创建一个”:是的
    猜你喜欢
    • 2015-09-14
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2011-05-19
    相关资源
    最近更新 更多