kuemme01 kuemme01 - 1 year ago 50
Swift Question

button load from xib via loadNibNamed() never called

Another problem arrived me. I have a view covering my hole screen. In it a webView for showing a youTube video and a button for closing. The view is load via loadNibNamed() and no matter what I'm doing, my IBAction never called by pressing that button.
I tried to make an IBAction via storyboard, also via addTarget to a buttonoutlet. Both never called. My button is reachable when I click it, it show the default touchanimation. Also printing the button outlet in viewDidLoad() says

<UIButton: 0x17d25430; frame = (15 8; 39 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x17d25ae0>>
so it is not nil.

My Layout looks like the following.

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

    • In one of mainScreens tableView is a customCell named videoViewCell implemented with:

    let cell = NSBundle.mainBundle().loadNibNamed("VideoViewCell", owner: self, options: nil)!.first as! VideoViewCell

videoViewCell: UITableViewCell , UITableViewDelegate, UITableViewDataSource {} has:

weak var delegate: ActivityDetailVideoCellDelegate?


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


protocol ActivityDetailVideoCellDelegate: class {
func selectedCellAtIndex(index: Int)

My mainScreen class has:

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)


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


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

my xib:

Answer Source

Finally solved my Problem by writing the @IBAction func closeButtonClicked(sender: AnyObject) {} inside my MainScreenViewController an adding Target inside func selectedCellAtIndex(index: Int) {} to call my youtubePopUp inside closeButtonClicked I made it global:

@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