我有一个名为bookviewc的viewcontroller,它只显示一个视频。故事板包含一个名为fullname的iboutlet和一个用于显示视频的uiview。我有一个功能,当你点击视频,它应该得到下一个视频。我总是得到全名的nil值当我得到下一个视频时,它总是正确地得到第一个视频。
线程1:致命错误:在展开
可选值
在iBullet上输入全名,我知道为什么,有办法吗?这是我的代码

class BookViewC: UIViewController, VideoViewDelegate{

@IBOutlet weak var VideoView: UIView!
@IBOutlet weak var FullName: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()

  // This method queries the database for the latest video
  ReloadTable(Offset: 0)

}

 func reloadTable(Offset: Int) {
 // Queries the database for video name
 // Returns data in Json
        DispatchQueue.main.async {
                    for Stream in Streams! {

                        if let fullname = Stream["fullname"] as? String            {
    // This below throws the nil error when getting the 2nd video
                            self.FullName.setTitle(fullname, for: .normal)

                        }

}
   // Method below simply shows the video
   self.PlayVideo(MediaHeight: Float(pic_height), MediaURL: s_image)
  }

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()
}

}
当你点击一段视频时,它就会转到这个班
class CustomAVPLayerC: AVPlayerViewController {


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


            // I try to return to my original Class now
            let BookC = BookViewC()
            for touch in touches {
                let location = touch.location(in: self.view)

                if location.x <  self.view.layer.frame.size.width / 2 {
                    print("Tapped Left")
                }
                else {
                    print("Tapped Right get next video")
                // Offset is now 1 which gets the 2nd video
                   BookC.ReloadTable(offset: 1)
                }
            }



    }


}

发生的事情是,一旦我转到customavplayerc控制器,bookviewc中的所有iboutlet都将变为nil,然后一旦我试图返回,它就会给我错误。我能做些什么来解决这个问题吗?我显示的视频使用的是avplayer,所以点击后我必须转到customavplayerc控制器。任何建议都很好。

最佳答案

IBOutlets为零,因为您的BookVC不是从.xib/storyboard初始化的。使用委托模式应该可以解决这个问题。下面是一个有助于实现这一目标的例子,
创建如下协议,

protocol CustomAVPLayerProtocol: class {
    func reloadTable(at index: Int)
}

更新BookViewC以符合下面的CustomAVPLayerProtocol
extension BookViewC: CustomAVPLayerProtocol {

    func reloadTable(at index: Int) {
       self.reloadTable(Offset: index)
    }
}

然后更新CustomAVPLayerC
class CustomAVPLayerC: AVPlayerViewController {

   weak var delegate: CustomAVPLayerProtocol?

   func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let location = touch.location(in: self.view)

            if location.x <  self.view.layer.frame.size.width / 2 {
                print("Tapped Left")
            } else {
                let index = 1
                self.delegate?.reloadTable(at: index)
            }
        }
    }
}

最后在BookViewC中设置CustomAVPLayerC(初始化时)如下委托,
let customAVPLayerVC = CustomAVPLayerC()
customAVPLayerVC.delegate = self

关于swift - iOS Swift如何致命错误解开可选值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49767246/

10-13 03:59