swift 和 View Controller 范例的新手。想知道 ViewControllers 是否自动带有它描述的对象,例如,avplayerviewcontroller 是否自动带有准备使用的 avplayer 对象,或者我们是否仍然需要创建一个 avplayer 对象?另一个例子可能是 uitableviewcontroller,它是自动带有一个 tableview 对象还是我们需要在创建一个 uitableviewcontroller 的子类的 cocoa touch 类之后创建它。
下面是一些编译示例代码,我不一定要创建 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()
}
}
}
最佳答案
是的。 XYFeatureViewControllers 是方便的 UIViewController
子类。例如 UITableViewController
继承自 UIViewController
,假设它的 view
是 UITableView
类型并且已经符合 UITableViewDelegate
和 UITableViewDataSource
协议(protocol)。
通常他们会为委托(delegate)方法提供默认实现,因此可以立即“正常工作”。
在 UITableView
的情况下,我建议使用自定义 UIViewController
并自己添加 UITableView
(s) 以获得完全的灵 active .
例如,UITableViewController
将 UITableView
作为 Root View ,因此如果您稍后想要在屏幕上缩小 tableView(示例),则不能。
在您的自定义 viewController 中,tableView 将是 rootView 的 subview ,因此调整它的大小或在它旁边添加第二个都不是问题。
关于ios - AVPlayerViewController 是否自动带有 AVPlayer 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40452413/