简体   繁体   English

AVPlayerViewController是否自动带有AVPlayer对象?

[英]Does the AVPlayerViewController automatically come with a AVPlayer object?

New to swift and the view controller paradigm. 快速入门和视图控制器范例。 Would like to know if ViewControllers automatically come with the objects it describes for example, does avplayerviewcontroller automatically come with an avplayer object ready to be used or do we still need to create an avplayer object? 是否想知道ViewControllers是否自动随附了它所描述的对象,例如,avplayerviewcontroller是否自动附带了一个可供使用的avplayer对象,还是我们仍然需要创建avplayer对象? Another example could be uitableviewcontroller, does it automatically come with a tableview object or do we need to create it after creating a cocoa touch class that subclasses uitableviewcontroller. 另一个示例可能是uitableviewcontroller,它是自动与tableview对象一起提供的,还是我们需要在创建可继承uitableviewcontroller的可可接触类之后创建它。

Here is some example code that compiles where I didn't necessarily create an avplayer object but it seems like that object was already ready to be used through a property called 'player': 这是一些示例代码,在这些代码中,我不一定要创建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()


        }
    }


}

Yes. 是。 XYFeatureViewControllers are convenience UIViewController subclasses. XYFeatureViewControllers是便利的UIViewController子类。 For example UITableViewController inherits from UIViewController , assumes its view is of type UITableView and already conforms to the UITableViewDelegate and UITableViewDataSource protocols. 例如, UITableViewController继承自UIViewController ,假定其viewUITableView类型,并且已经符合UITableViewDelegateUITableViewDataSource协议。

Often they offer default implementations for delegate methods and thus "just work" right away. 通常,它们为委托方法提供默认实现,从而立即“正常工作”。

In case of a UITableView , I would recommend to use a custom UIViewController an add UITableView (s) yourself to have the full flexibility. 如果使用UITableView ,我建议自己使用自定义UIViewController和添加UITableView以具有完全的灵活性。

For Example, UITableViewController has the UITableView as root view, so if you want to make the tableView smaller on screen later on (example), you can't. 例如, UITableViewController具有UITableView作为根视图,因此,如果您想稍后在屏幕上缩小tableView(示例),则不能这样做。

In your custom viewController, the tableView would be a subView of the rootView, so resizing it or adding a second one next to it would not be a problem. 在您的自定义viewController中,tableView将是rootView的子视图,因此调整它的大小或在其旁边添加第二个视图将不是问题。

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

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