【发布时间】:2021-01-04 23:57:19
【问题描述】:
我正在以编程方式创建一个按钮并在我的 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)
【问题讨论】:
-
您收到的完整错误信息是什么?
-
...close Action] 无法识别的选择器发送到实例 0x7ffd57c208b0
-
也意识到我需要删除“关闭按钮”
标签: swift xcode uibutton instance extension-methods