【问题标题】:button load from xib via loadNibNamed() never called通过 loadNibNamed() 从 xib 加载按钮从未调用
【发布时间】:2016-09-11 17:18:55
【问题描述】:

我遇到了另一个问题。我有一个覆盖我的孔屏幕的视图。其中有一个用于显示 YouTube 视频的 webView 和一个用于关闭的按钮。该视图是通过 loadNibNamed() 加载的,无论我在做什么,我的 IBAction 都不会通过按下该按钮来调用。 我试图通过情节提要制作 IBAction,也通过 addTarget 到按钮插座。两人都没有打电话。当我单击它时,我的按钮可以访问,它显示默认的触摸动画。还在 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,其实现方式为:

    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:

【问题讨论】:

  • 查看您的视图层次结构,看起来您的关闭按钮是您的网络视图的子视图。尝试将其从 webview 中拉出并成为您的视图或背景视图的子视图
  • 是的,也是我的第一个怀疑,但它与 webView 处于同一级别,不同的顺序给出相同的结果
  • userInteractionEnabled 绝对不是false,是吗?
  • 只是测试它,将其手动设置为 true,但这没有任何改变
  • myCustomCellUITableViewCell 还是 UIViewController

标签: ios swift xib ibaction loadnibnamed


【解决方案1】:

终于解决了我的问题,方法是在我的 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()
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2017-03-27
    • 1970-01-01
    • 2019-10-01
    • 2017-07-22
    • 1970-01-01
    相关资源
    最近更新 更多