![](/img/trans.png)
[英]Storyboard, drag link to an IBAction from a container view to the parent
[英]How to make @IBAction from a custom view in storyboard?
我在viewController
添加了一個帶有按鈕的自定義視圖。 我想打一個@IBAction
從按鈕到我viewController
。 我有以下問題:
@IBAction
從按鈕到我viewController
? class CustomCellView: UIView{
//MARK: Initializer
override func awakeFromNib() {
super.awakeFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
commonInit()
}
//MARK: Functions
func commonInit(){
let customView = Bundle.main.loadNibNamed("customCellView", owner: self, options: nil)?.first as? UIView ?? UIView()
customView.frame = self.bounds
self.addSubview(customView)
}
}
在添加了@objc
的viewController
中添加customView
。
@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
}
我的customView沒有顯示在情節提要中,但是當我運行該應用程序時就可以了
這是自定義視圖的預期行為。 您將不會看到僅在定義視圖的xib中使用自定義視圖的xib中的視圖外觀。
如何使@IBAction從該按鈕到我的viewController。
你不能 自定義視圖不會在界面生成器中公開子視圖。 最好的辦法是使用委托模式,或者正如Bhavesh Sarsawa指出的那樣,通過將vc添加為目標,或者通過在ccustom視圖中創建IBAction來調用該模式,以調用依賴注入發送的閉包。
customButton.setAction { code that gets called when the button is pressed }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.