Ios 如何在主VC中访问自定义视图按钮操作

Ios 如何在主VC中访问自定义视图按钮操作,ios,swift,uiview,uibutton,xib,Ios,Swift,Uiview,Uibutton,Xib,我已经创建了一个自定义视图xib并给出了该视图类。现在我在主vc中获取一个视图并给出该类,但现在我想在主vc中访问自定义视图按钮操作方法。那我该怎么做呢 这是我的自定义视图 import UIKit class TextCustomisationVC: UIView { @IBOutlet var contentView: UIView! override init(frame: CGRect) { super.init(frame: frame)

我已经创建了一个自定义视图xib并给出了该视图类。现在我在主vc中获取一个视图并给出该类,但现在我想在主vc中访问自定义视图按钮操作方法。那我该怎么做呢

这是我的自定义视图

import UIKit

class TextCustomisationVC: UIView {
    @IBOutlet var contentView: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.commonInit()
    }

    private func commonInit(){
        Bundle.main.loadNibNamed("TextCustomisationVC", owner: self, options: nil)
        addSubview(contentView)
        contentView.frame =  self.bounds
        contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    }

    @IBAction func btnCloseCustomisation_Click(_ sender: Any) {
    }

    @IBAction func btnApplyCustomisation_Click(_ sender: Any) {
    }
}

现在我在我的主VC中创建了一个outlet,并给了同一个类,我可以访问这些类outlet,但现在我想访问上面的按钮操作方法,那么我该怎么做呢?

你可以试试

let cusView = TextCustomisationVC(frame:///)
如果在函数内部使用btn发送器

cusView.btnCloseCustomisation_Click(cusView.closeBtn)
否则发送任何虚拟按钮

cusView.btnCloseCustomisation_Click(UIButton())
编辑:

protocol CustomTeller {
    func closeClicked(UIButton)
}

class TextCustomisationVC: UIView {

    var delegate: CustomTeller?

    @IBAction func btnCloseCustomisation_Click(_ sender: UIButton) {
        self.delegate?.closeClicked(sender:sender)
    }
}
//在缅因州

let cusView = TextCustomisationVC(frame:///)
cusView.delegate = self
实施

func closeClicked(sender:UIButton) {

      // close button called 

}

您可以在这里使用委托,它可以在主VC中实现

创建如下协议:

protocol ButtonActionDelegate {
   func closeButtonPressed(_ sender:UIButton)
   func applyButtonPressed(_ sender:UIButton)
}
var delegate:ButtonActionDelegate?
extension mainVC : ButtonActionDelegate {

    func closeButtonPressed(_ sender: UIButton) {
    }
    func applyButtonPressed(_ sender: UIButton) {
    }

}
@IBAction func btnCloseCustomisation_Click(_ sender: Any) {
    self.delegate?.closeButtonPressed(sender)
}

@IBAction func btnApplyCustomisation_Click(_ sender: Any) {
    self.delegate?.applyButtonPressed(sender)
}
然后在视图中创建委托的实例,如下所示:

protocol ButtonActionDelegate {
   func closeButtonPressed(_ sender:UIButton)
   func applyButtonPressed(_ sender:UIButton)
}
var delegate:ButtonActionDelegate?
extension mainVC : ButtonActionDelegate {

    func closeButtonPressed(_ sender: UIButton) {
    }
    func applyButtonPressed(_ sender: UIButton) {
    }

}
@IBAction func btnCloseCustomisation_Click(_ sender: Any) {
    self.delegate?.closeButtonPressed(sender)
}

@IBAction func btnApplyCustomisation_Click(_ sender: Any) {
    self.delegate?.applyButtonPressed(sender)
}
在主VC中实现此委托,如下所示:

protocol ButtonActionDelegate {
   func closeButtonPressed(_ sender:UIButton)
   func applyButtonPressed(_ sender:UIButton)
}
var delegate:ButtonActionDelegate?
extension mainVC : ButtonActionDelegate {

    func closeButtonPressed(_ sender: UIButton) {
    }
    func applyButtonPressed(_ sender: UIButton) {
    }

}
@IBAction func btnCloseCustomisation_Click(_ sender: Any) {
    self.delegate?.closeButtonPressed(sender)
}

@IBAction func btnApplyCustomisation_Click(_ sender: Any) {
    self.delegate?.applyButtonPressed(sender)
}
然后您可以分别如下所示调用委托方法:

protocol ButtonActionDelegate {
   func closeButtonPressed(_ sender:UIButton)
   func applyButtonPressed(_ sender:UIButton)
}
var delegate:ButtonActionDelegate?
extension mainVC : ButtonActionDelegate {

    func closeButtonPressed(_ sender: UIButton) {
    }
    func applyButtonPressed(_ sender: UIButton) {
    }

}
@IBAction func btnCloseCustomisation_Click(_ sender: Any) {
    self.delegate?.closeButtonPressed(sender)
}

@IBAction func btnApplyCustomisation_Click(_ sender: Any) {
    self.delegate?.applyButtonPressed(sender)
}

您是否尝试过在主VC中直接执行这些操作?然后,您可以使用
addTarget(uu,action:,for:)
将目标操作添加到主VC中的按钮。我可以这样做,但我必须为此设置出口并创建该按钮的自定义操作,我想访问在xib中创建的相同操作以访问主VC。您的问题尚不清楚。您希望在主VC中获取操作,但也尝试从自定义视图访问操作方法。这让人困惑。好吧,我在我的主vc中点击了cusView.btnCloseCustomization\u,但我必须传递什么作为参数?好吧,现在当我点击Xib中的按钮时,我如何才能进入我的主vc?根据你的回答,我提前得到了这个行动,没有点击。你需要使用委托人。你能发布详细的回答吗?你想说我必须为此创建协议吗?错过了一个步骤,即当我们在mainVC中初始化UIView时,需要设置委托。customeView.delegate=self,否则它将无法工作。