IOS Swift 协议不工作或遇到断点
IOS Swift Protocol is not working or hitting breakpoint
我几天前提出了一个问题,并获得了关于如何解决来回传递数据问题的协议。我还查看了一些教程并创建了一个协议,但它没有工作,甚至从我所看到的它应该工作的角度来看,它甚至达到了断点。我已经为我的 AVPlayer 创建了一个协议,这样一来它就可以获得一个新视频,但就像我说的那样它甚至没有达到断点这是我的代码...
protocol CustomAVPLayerProtocol: class {
func reloadTabled(at index: Int)
}
class CustomAVPLayerC: AVPlayerViewController {
var delagate: CustomAVPLayerProtocol?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.delagate?.reloadTabled(at: 1)
for touch in touches {
self.delagate?.reloadTabled(at: 1)
print("Tapped")
// When I tap the AVPlayer this print statement shows
// So I know it is coming here
}
}
}
现在这是我的第二个class/controller
class BookViewC: UIViewController, CustomAVPLayerProtocol {
override func viewDidLoad() {
super.viewDidLoad()
PlayVideo(250, "url")
}
func reloadTabled(at index: Int) {
print("This protocol method does not execute or hit breakpoint")
self.PlayVideo(250, "url")
}
func PlayVideo(MediaHeight: Float, MediaURL: String) {
let movieURL = URL(string: MediaURL)
videoCapHeight.constant = CGFloat(MediaHeight)
streamsModel.playerView = AVPlayer(url: movieURL!)
streamsModel.MyAVPlayer.player = streamsModel.playerView
streamsModel.MyAVPlayer.videoGravity = AVLayerVideoGravity.resizeAspectFill.rawValue
streamsModel.MyAVPlayer.showsPlaybackControls = false
streamsModel.MyAVPlayer.view.frame = VideoView.bounds
VideoView.addSubview(streamsModel.MyAVPlayer.view)
self.addChildViewController(streamsModel.MyAVPlayer)
streamsModel.playerView?.isMuted = false
streamsModel.MyAVPlayer.player?.play()
}
}
正如我之前所说,它甚至没有在 BookViewC.reloadTabled 上遇到断点,因为建议会很好
协议是不相关对象相互通信时最常用的手段。根据上面的代码,通信似乎没有发生。
你的协议声明部分似乎没问题。问题存在于 secondViewController 中。我可以看到您没有将委托设置为已创建的对象。理想情况下,它必须是这样的:
Class BookViewC: UIViewController, CustomAVPLayerProtocol {
override func viewDidLoad() {
super.viewDidLoad()
PlayVideo(250, "url")
}
您需要在此处设置委托:
func PlayVideo {
let customPlayer = CustomAVPLayerC()
customPlayer.delegate = self //This makes the selector to respond
}
根据您的代码,这些是一些小错误,您可以更正这些错误以使其正常工作。
1. `weak var delagate: CustomAVPLayerProtocol?`
*Make a weak delegate to save it from retaining a strong reference cycle and memory leaks.*
2. Code Snippet:
func PlayVideo {
let customPlayer = CustomAVPLayerC()
customPlayer.delegate = self
}
在你的第二个 ViewController 中,你需要将你的委托分配给一个对象/视图控制器以使其响应
NOTE: In case you require, you can make a super class that conforms the your protocol class, so that your every view controller conforms it automatically, you just need to assign an delegate to class on which you want to use it.
您的基础设置正确,但请记住 类(大部分)只是实例的蓝图。这些 类 在您创建它们的实例之前是无用的,因为它将完成工作。
因此,只需传递一个实例作为另一个实例的委托,您可以在此处执行此操作,因为您已正确设置协议。
protocol CustomAVPLayerProtocol: AnyObject {
func reloadTabled(at index: Int)
}
class CustomAVPLayerC: AVPlayerViewController {
weak var delagate: CustomAVPLayerProtocol?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.delagate?.reloadTabled(at: 1)
for touch in touches {
self.delagate?.reloadTabled(at: 1)
print("Tapped")
}
}
}
class BookViewC: UIViewController, CustomAVPLayerProtocol {
override func viewDidLoad() {
super.viewDidLoad()
PlayVideo(250, "url")
}
func reloadTabled(at index: Int) {
PlayVideo(250, "url")
}
func PlayVideo(_ MediaHeight: Float, _ MediaURL: String) {
//
}
}
let book = BookViewC()
let layer = CustomAVPLayerC()
layer.delagate = book
在哪里执行此操作 instantiation/delegation 由您决定。另外,我知道这里很多人使用 class
来定义仅符合 类 的协议,但是 Swift 的文档指示我们使用 AnyObject
.
我几天前提出了一个问题,并获得了关于如何解决来回传递数据问题的协议。我还查看了一些教程并创建了一个协议,但它没有工作,甚至从我所看到的它应该工作的角度来看,它甚至达到了断点。我已经为我的 AVPlayer 创建了一个协议,这样一来它就可以获得一个新视频,但就像我说的那样它甚至没有达到断点这是我的代码...
protocol CustomAVPLayerProtocol: class {
func reloadTabled(at index: Int)
}
class CustomAVPLayerC: AVPlayerViewController {
var delagate: CustomAVPLayerProtocol?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.delagate?.reloadTabled(at: 1)
for touch in touches {
self.delagate?.reloadTabled(at: 1)
print("Tapped")
// When I tap the AVPlayer this print statement shows
// So I know it is coming here
}
}
}
现在这是我的第二个class/controller
class BookViewC: UIViewController, CustomAVPLayerProtocol {
override func viewDidLoad() {
super.viewDidLoad()
PlayVideo(250, "url")
}
func reloadTabled(at index: Int) {
print("This protocol method does not execute or hit breakpoint")
self.PlayVideo(250, "url")
}
func PlayVideo(MediaHeight: Float, MediaURL: String) {
let movieURL = URL(string: MediaURL)
videoCapHeight.constant = CGFloat(MediaHeight)
streamsModel.playerView = AVPlayer(url: movieURL!)
streamsModel.MyAVPlayer.player = streamsModel.playerView
streamsModel.MyAVPlayer.videoGravity = AVLayerVideoGravity.resizeAspectFill.rawValue
streamsModel.MyAVPlayer.showsPlaybackControls = false
streamsModel.MyAVPlayer.view.frame = VideoView.bounds
VideoView.addSubview(streamsModel.MyAVPlayer.view)
self.addChildViewController(streamsModel.MyAVPlayer)
streamsModel.playerView?.isMuted = false
streamsModel.MyAVPlayer.player?.play()
}
}
正如我之前所说,它甚至没有在 BookViewC.reloadTabled 上遇到断点,因为建议会很好
协议是不相关对象相互通信时最常用的手段。根据上面的代码,通信似乎没有发生。
你的协议声明部分似乎没问题。问题存在于 secondViewController 中。我可以看到您没有将委托设置为已创建的对象。理想情况下,它必须是这样的:
Class BookViewC: UIViewController, CustomAVPLayerProtocol {
override func viewDidLoad() {
super.viewDidLoad()
PlayVideo(250, "url")
}
您需要在此处设置委托:
func PlayVideo {
let customPlayer = CustomAVPLayerC()
customPlayer.delegate = self //This makes the selector to respond
}
根据您的代码,这些是一些小错误,您可以更正这些错误以使其正常工作。
1. `weak var delagate: CustomAVPLayerProtocol?`
*Make a weak delegate to save it from retaining a strong reference cycle and memory leaks.*
2. Code Snippet:
func PlayVideo {
let customPlayer = CustomAVPLayerC()
customPlayer.delegate = self
}
在你的第二个 ViewController 中,你需要将你的委托分配给一个对象/视图控制器以使其响应
NOTE: In case you require, you can make a super class that conforms the your protocol class, so that your every view controller conforms it automatically, you just need to assign an delegate to class on which you want to use it.
您的基础设置正确,但请记住 类(大部分)只是实例的蓝图。这些 类 在您创建它们的实例之前是无用的,因为它将完成工作。
因此,只需传递一个实例作为另一个实例的委托,您可以在此处执行此操作,因为您已正确设置协议。
protocol CustomAVPLayerProtocol: AnyObject {
func reloadTabled(at index: Int)
}
class CustomAVPLayerC: AVPlayerViewController {
weak var delagate: CustomAVPLayerProtocol?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.delagate?.reloadTabled(at: 1)
for touch in touches {
self.delagate?.reloadTabled(at: 1)
print("Tapped")
}
}
}
class BookViewC: UIViewController, CustomAVPLayerProtocol {
override func viewDidLoad() {
super.viewDidLoad()
PlayVideo(250, "url")
}
func reloadTabled(at index: Int) {
PlayVideo(250, "url")
}
func PlayVideo(_ MediaHeight: Float, _ MediaURL: String) {
//
}
}
let book = BookViewC()
let layer = CustomAVPLayerC()
layer.delagate = book
在哪里执行此操作 instantiation/delegation 由您决定。另外,我知道这里很多人使用 class
来定义仅符合 类 的协议,但是 Swift 的文档指示我们使用 AnyObject
.