无法识别的选择器发送到实例-UI按钮-Swift



我正在以编程方式创建一个按钮,并在VC的扩展中添加一个操作;删除";视图而不是隐藏它们

func() setUpViews { 
//views
let shadowView = UIView(frame: CGRect(x: Int((UIScreen.main.bounds.width) - CGFloat(width)) / 2, 
y: (Int(UIScreen.main.bounds.height)), width: width, height: height))
shadowView.backgroundColor = .none
let mainView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height))
let blackView =  UIView(frame: CGRect(x: 0, y: Int(self.navBar.bounds.height), width: 
Int(UIScreen.main.bounds.width), height: Int(self.rewardsTableView.bounds.height)))
shadowView.setUpViewShadowForCell()
blackView.backgroundColor = .black
blackView.alpha = 0
mainView.backgroundColor = color
mainView.layer.cornerRadius = 10
mainView.clipsToBounds = true
mainView.alpha = 1


//close Button
let closeButton = UIButton(frame: CGRect(x: CGFloat((Int(UIScreen.main.bounds.width) - width) / 
2), y: CGFloat((Int(UIScreen.main.bounds.height) - height) / 2) + mainView.bounds.height, width: 
mainView.bounds.width, height: 50))
closeButton.backgroundColor = UIColor.black.withAlphaComponent(0.8)
closeButton.tintColor = .white
closeButton.setTitle("Close", for: .normal)
closeButton.layer.cornerRadius = 10
closeButton.addTarget(self, action: Selector(("closeAction")), for: .touchDown)

这是我试图运行的函数。从本质上讲,我想删除显示之外的视图。目前,我正在隐藏它们,并且我得到了错误";无法识别的选择器";。

//function to revert back to original, currently hiding
func closeAction(sender: AnyObject) {
blackView.isHidden = true
mainView.isHidden = true
}
//adding the views
self.view.addSubview(shadowView)
self.view.addSubview(blackView)
self.view.addSubview(closeButton)
shadowView.addSubview(mainView)
self.view.bringSubviewToFront(shadowView)

}

尝试过这个:

@objc func closeAction(sender: UIButton, UIView: UIView, UIView2: 
UIView, 
Button1: UIButton) {
UIView.removeFromSuperview()
UIView2.removeFromSuperview()
Button1.removeFromSuperview()
}

而在实际功能中:

closeAction(sender: closeButton, UIView: shadowView, UIView2: 
mainView, Button1: closeButton)
closeButton.addTarget(self, action: #selector(closeAction), for: 
.touchDown)

首先使用正确的语法

#selector(closeAction)

其次,您必须将操作标记为@objc,建议使用发送者的真实类型,而不是未指定的AnyObject

@objc func closeAction(_ sender: UIButton) {

要删除按钮,请将其从superView中删除

相关内容

最新更新