1

另一个问题来了。我有一个覆盖我的孔屏幕的视图。其中有一个用于显示 YouTube 视频的 webView 和一个用于关闭的按钮。该视图是通过 loadNibNamed() 加载的,无论我在做什么,我的 IBAction 都不会通过按下该按钮来调用。我试图通过情节提要制作 IBAction,也通过 addTarget 到按钮插座。两人都没有打电话。当我单击它时,我的按钮可以访问,它显示默认的触摸动画。同样在 viewDidLoad() 中打印按钮出口表示<UIButton: 0x17d25430; frame = (15 8; 39 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x17d25ae0>>它不是零。

我的布局如下所示。

  • 主屏幕:UIViewController、UITableViewDelegate、UITableViewDataSource、UIWebViewDelegate{}

    • 在其中一个 mainScreens tableView 中是一个名为 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()
    }
}

我的xib: 西布

4

1 回答 1

0

最后通过在我的 MainScreenViewController 内部编写@IBAction func closeButtonClicked(sender: AnyObject) {}一个添加目标func selectedCellAtIndex(index: Int) {}来在 closeButtonClicked 内部调用我的 youtubePopUp 来解决我的问题,我将其设为全局:

@IBAction func closeButtonClicked(sender: AnyObject) {
    print("close clicked")

    youtubePopup!.webView.loadHTMLString("about:blank", baseURL: nil)
    youtubePopup!.webView.mediaPlaybackRequiresUserAction = false

    UIView.animateWithDuration(0.25, animations: { () -> Void in
        self.youtubePopup!.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
        self.youtubePopup!.view.alpha = 0
    }) { (finished) -> Void in
        self.youtubePopup!.view.removeFromSuperview()
    }

}
于 2016-09-15T17:54:57.103 回答