【问题标题】:Unrecognized Selector Sent to Instance - UI Button - Swift无法识别的选择器发送到实例 - UI 按钮 - Swift
【发布时间】: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


【解决方案1】:

首先使用正确的语法

#selector(closeAction)

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

@objc func closeAction(_ sender: UIButton) {

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

【讨论】:

  • 好的,我明白了:(at symbol)objc 只能与类成员、@objc 协议和类的具体扩展一起使用
  • 动作方法必须声明在类的顶层,而不是在另一个方法中
  • 感谢瓦迪安。一旦我把它移到课外;由于扩展不能保存变量/常量,我如何调用函数中创建的视图?
  • 不在课堂之外,在课堂或扩展的顶层,请在我的回答中完全使用建议的语法。无法添加更多参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 2018-04-24
相关资源
最近更新 更多