从xib通过loadNibNamed()从未调用的按钮加载

时间:2016-09-11 17:18:55

标签: ios swift xib ibaction loadnibnamed

另一个问题来自我。我有一个覆盖我的洞屏幕的视图。在其中一个webView用于显示youTube视频和一个关闭按钮。视图是通过loadNibNamed()加载的,无论我做什么,我的IBAction都不会通过按下该按钮来调用。 我试图通过故事板进行IBAction,也可以通过addTarget到一个buttonoutlet。两人都没说过。单击它时,我的按钮可以访问,它显示默认的touchanimation。另外在viewDidLoad()中打印按钮插座说<UIButton: 0x17d25430; frame = (15 8; 39 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x17d25ae0>>所以它不是零。

我的布局如下所示。

  • MainScreen:UIViewController,UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate {}

    • 在其中一个mainScreens中,tableView是一个名为videoViewCell的customCell,实现时为:

    让cell = NSBundle.mainBundle()。loadNibNamed(&#34; VideoViewCell&#34 ;, owner:self,options:nil)!. first first as! VideoViewCell

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: xib

1 个答案:

答案 0 :(得分:0)

最后通过在我的MainScreenViewController中编写@IBAction func closeButtonClicked(sender: AnyObject) {}并在func selectedCellAtIndex(index: Int) {}内添加Target以在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()
    }

}