別の問題が私に届きました。ホールスクリーンをカバーするビューがあります。その中には、YouTube ビデオを表示するための webView と、閉じるためのボタンがあります。ビューは loadNibNamed() を介してロードされ、何をしていても、そのボタンを押しても IBAction が呼び出されることはありません。ストーリーボードを介して IBAction を作成しようとしましたが、addTarget をボタンアウトレットにも使用しました。どちらも電話したことはありません。ボタンをクリックすると到達可能になり、デフォルトのタッチアニメーションが表示されます。また、viewDidLoad() でボタン アウトレットを印刷すると、<UIButton: 0x17d25430; frame = (15 8; 39 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x17d25ae0>>
nil ではないことがわかります。
私のレイアウトは次のようになります。
メインスクリーン: UIViewController、UITableViewDelegate、UITableViewDataSource、UIWebViewDelegate{}
- mainScreens tableView の 1 つは、次のもので実装された videoViewCell という名前の customCell です。
let cell = NSBundle.mainBundle().loadNibNamed("VideoViewCell", owner: self, options: nil)!.first as! ビデオ ビュー セル
videoViewCell: UITableViewCell 、 UITableViewDelegate、UITableViewDataSource {} には:
weak var delegate: ActivityDetailVideoCellDelegate?
...
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
delegate?.selectedCellAtIndex(indexPath.row)
}
...
protocol ActivityDetailVideoCellDelegate: class {
func selectedCellAtIndex(index: Int)
}
私の mainScreen クラスには次のものがあります。
extension MainScreen: ActivityDetailVideoCellDelegate {
func selectedCellAtIndex(index: Int) {
let youtubeLink: String = "http://www.youtube.com/embed/\(selectedVideo)"
let youtubePopup = YouTubePopUpController()
youtubePopup.view.frame = self.view.bounds
youtubePopup.view.alpha = 0
youtubePopup.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
let code: String = "<iframe width=\(youtubePopup.webView.bounds.width) height=\(youtubePopup.webView.bounds.height) src=\(youtubeLink) frameborder=0 allowfullscreen></iframe> <style>body { margin: 0; padding: 0; }</style>"
youtubePopup.webView.loadHTMLString(code, baseURL: nil)
self.view.addSubview(youtubePopup.view)
UIView.animateWithDuration(0.25, animations: { () -> Void in
youtubePopup.view.alpha = 1
youtubePopup.view.transform = CGAffineTransformMakeScale(1, 1)
})
}
}
YouTubePopUpController: UIViewController:
@IBOutlet weak var backgroundView: UIView!
@IBOutlet weak var webView: UIWebView!
@IBOutlet weak var closeButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
print(closeButton)
closeButton.addTarget(self, action: #selector(self.closeButtonClicked(_:)), forControlEvents: .TouchUpInside)
}
@IBAction func closeButtonClicked(sender: AnyObject) {
print("close clicked")
UIView.animateWithDuration(0.25, animations: { () -> Void in
self.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
self.view.alpha = 0
}) { (finished) -> Void in
self.view.removeFromSuperview()
}
}