Ios 如何从故事板中的自定义视图生成@iAction?

Ios 如何从故事板中的自定义视图生成@iAction?,ios,swift,xib,ibaction,custom-view,Ios,Swift,Xib,Ibaction,Custom View,我在viewController中添加了一个自定义视图,该视图有一个按钮。我想从那个按钮到我的viewController做一个@iAction。 我有以下问题: 我如何让它出现在故事板上?当我运行应用程序时,它工作正常。 如何从该按钮到我的viewController生成@iAction? 类CustomCellView:UIView{ //标记:初始值设定项 重写函数awakeFromNib{ super.awakeFromNib } 重写initframe:CGRect{ super.in

我在viewController中添加了一个自定义视图,该视图有一个按钮。我想从那个按钮到我的viewController做一个@iAction。 我有以下问题:

我如何让它出现在故事板上?当我运行应用程序时,它工作正常。 如何从该按钮到我的viewController生成@iAction? 类CustomCellView:UIView{ //标记:初始值设定项 重写函数awakeFromNib{ super.awakeFromNib } 重写initframe:CGRect{ super.initframe:frame 普通的 } 所需初始编码:NSCoder{ super.initcoder:coder 普通的 } 重写func PrepareFormonterFaceBuilder{ super.prepareforPrinterFaceBuilder 普通的 } //马克:功能 func commonInit{ 让customView=Bundle.main.loadNibNamedcustomCellView,所有者:self,选项:nil?。首先作为?UIView?UIView customView.frame=self.bounds self.addSubviewcustomView } } 在添加customView的viewController中添加@objc

例如 使用 我的customView没有显示在故事板中,但当我运行应用程序时,它就可以了

这是自定义视图的预期行为。只有在定义视图的xib中使用自定义视图时,才能看到视图在xib中的外观

如何将@iAction从该按钮发送到我的viewController

你不能。自定义视图不会在interface builder中公开子视图。您可以做的最好的事情是使用委托模式,或者像Bhavesh Sarsawa指出的那样,通过编程方式,或者通过添加vc作为目标,或者通过在ccustom视图中生成iAction来调用依赖项注入发送的闭包


您忘了将此视图设置为可设计视图?您可能不需要创建@IBAction。只需创建一个可选的闭包并从viewController提供它,并从CustomCellView类中的@iAction'调用闭包。我将其设置为IBDesignable,但结果相同。我需要viewController中的@iAction来处理一些数据。我在这篇文章中找到了答案:commonInit中的actionCustom给出错误:使用未解析标识符“actionCustom”
    @objc func actionCustom()
    {
        print("Click Added") 
    }
func commonInit(){
      let customView = Bundle.main.loadNibNamed("customCellView", owner: self, options: nil)?.first as? UIView ?? UIView()
      customView.frame = self.bounds
      self.addSubview(customView)

      customView.button.addTarget(self, action: #selector(actionCustom), for: .touchUpInside) //Add click code here
  }

customButton.setAction { code that gets called when the button is pressed }