user6510422 user6510422 - 11 months ago 89
iOS Question

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

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':

import UIKit
import AVKit
import AVFoundation

class MeetTheAuthorViewController: UIViewController {

override func viewDidLoad() {
// 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!)



Answer Source

Yes. XYFeatureViewControllers are convenience UIViewController subclasses. For example UITableViewController inherits from UIViewController, assumes its view is of type UITableView and already conforms to the UITableViewDelegate and UITableViewDataSource protocols.

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.

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.

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.