iOS 自定义 View (.XIB) 按钮绑定(bind)不起作用

标签 ios swift xib

 protocol CustomViewAction: class {
  func customActionButton(_ sender: UIButton)
 }

 class MainFooter: UIView { 
   weak var delegate: CustomViewAction?

   @IBAction func languageButton(_ sender: UIButton) {
     self.delegate?.customActionButton(sender)
   } 
   private func loadNib() {...}
 }


class MainCont: CustomViewAction {
 ...
 let mainFooter = MainFooter()
 mainFooter.frame = view.frame
 mainFooter.delegate = self

 func customActionButton(_ sender: UIButton) {
  ... not working
 }
}

There is custom view. Custom action button interface implemented in the Main controller. This code not working ... Why?

最佳答案

这是非常糟糕的代码) 请修复

protocol CustomViewDelegate: class {
 func actionTouchCustomView(button:UIButton)
}


class MainFooterView: UIView { 
weak var delegate: CustomViewDelegate?

 //MARK: Handle Actions
@IBAction func handleTouchActionLanguageButton(_ sender: UIButton) {
 self.delegate?.actionTouchCustomView(sender)
} 

 //MARK: Life cycle
private func loadNib() {...}
}

class MainCont: CustomViewDelegate {
...
let mainFooter = MainFooter()
mainFooter.frame = view.frame
mainFooter.delegate = self

//MARK -  CustomViewDelegate
func actionTouchCustomView(button:UIButton) {
... not working
}
}
  1. 检查 XIB 或 Storyboard 中的链接按钮操作。
  2. 检查生命周期“let mainFooter”。也许对象已经死了

关于iOS 自定义 View (.XIB) 按钮绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53222420/

相关文章:

ios - 如何使 iOS 9 的 UIPopoverPresentationController 半透明

ios - 向 Game Center Xcode 6 报告分数

ios - Swift 框架二进制文件

ios - 删除单击任何其他字段或任何地方时的清除按钮

iphone - 圆形图像按钮 VS 方形 Controller

ios - 快速更改 CAShapeLayer 变换的动画持续时间

ios - 如何使用按钮放大/缩小 GoogleMap

swift - 将 UInt32 转换为通用 T : Numeric

ios - 如何在 objective c 中将 SOAP 数据发布到服务器

iphone - 如何在应用程序中切换本地化的 Nibs?