调用addTarget时,UIView类中的“无法识别的选择器已发送到实例”

亚历克斯·米纳斯扬

我试图创建一个DropDownMenu类,但是当我尝试将addTarget调用到按钮之一时,出现此错误。

无法识别的选择器发送到实例0x7fd167f06120',终止类型为NSException

我真的很感谢您的帮助,没有答案是不好的!

这是我全班

class DropDownMenu: UIView {
    // Main button or Pre
    var main: UIButton! = UIButton(frame: CGRect(x: 0, y: 0, width: 46, height: 30))
    var view: UIView!
    
    
    // Title
    var buttonTitles: [String]! = [""]
    var titleColor: UIColor! = UIColor.black
    var font: UIFont! = UIFont.systemFont(ofSize: 18)
    // Individual Button
    var buttonsBorderWidth: CGFloat! = 0
    var buttonsBorderColor: UIColor? = UIColor.white
    var buttonsCornerRadius: CGFloat! = 0
    var color: UIColor! = UIColor.clear
    // Button Images
    var buttonsImageEdgeInsets: UIEdgeInsets? = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    var images: [UIImage]? = nil
    // Onclick stuff
    var target: UIViewController!
    
    private var currentSelected: String? = nil
    
    private var optionsStack = UIStackView()
    
    init(main: UIButton) {
        self.main = main
        super.init(frame: CGRect())
    }
    
    func createDropDownMenu() {
        main.addTarget(target, action: #selector(DropDownMenu.openDropdown(_:)), for: .touchUpInside)
        
        print("Button Target?: \(main.allTargets), self.target: \(String(describing: target))")
        
        let mainFrame = main.frame
        
        optionsStack.frame = CGRect(x: mainFrame.minX, y: mainFrame.maxY, width: mainFrame.width, height: CGFloat(buttonTitles.count) * mainFrame.height)
        optionsStack.axis = .vertical
        
        view.addSubview(optionsStack)
                
        var y: CGFloat! = 0
        
        for title in buttonTitles {
            let button = UIButton(frame: CGRect(x: 0, y: y, width: mainFrame.width, height: mainFrame.height))
            button.setTitle(title, for: .normal)
            button.setTitleColor(titleColor, for: .normal)
            button.backgroundColor = color
            button.titleLabel?.font = font
            button.addTarget(target, action: #selector(DropDownMenu.onclick), for: .touchUpInside)
            
            y += mainFrame.height
            
            optionsStack.addArrangedSubview(button)
        }
        
        for button in optionsStack.arrangedSubviews {
            button.isHidden = true
            button.alpha = 0
        }
    }
    
    @objc private func openDropdown(_ sender: UIButton) {
        print("sender: \(String(describing: sender))")
        optionsStack.arrangedSubviews.forEach { (button) in
            UIView.animate(withDuration: 0.7) {
                button.isHidden = !button.isHidden
                button.alpha = button.alpha == 0 ? 1 : 0
                self.view.layoutIfNeeded()
            }
        }
    }
    
    @objc private func onclick(_ sender: UIButton) {
        let title = sender.titleLabel!.text
        
        print(title as Any)
        
        main.setTitle(title, for: .normal)
        
        optionsStack.arrangedSubviews.forEach { (button) in
            UIView.animate(withDuration: 0.7) {
                button.isHidden = true
                button.alpha = 0
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

这是ViewController中对象的代码和创建

let grade = UIButton(frame: CGRect(x: 50, y: 300, width: 80, height: 30))
grade.layer.borderWidth = 1
grade.setTitle("Grade", for: .normal)
grade.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
grade.setTitleColor(UIColor.black, for: .normal)
        
let gradeDP = DropDownMenu(main: main)
gradeDP.buttonTitles = ["Title 1", "Title 2", "Title 3"]
gradeDP.color = UIColor.gray
gradeDP.target = self
gradeDP.titleColor = UIColor.white
gradeDP.view = view

view.addSubview(grade)
gradeDP.createDropDownMenu()

createDropDownMenu()函数中的第一条打印语句将打印...

按钮目标?:[AnyHashable(<HomeworkHelp.DropDownMenu:0x7ffb555200b0; frame =(0 0; 0 0); layer = <CALayer:0x600002bdf5c0 >>)],self.target:可选(<HomeworkHelp.CreateAccountViewController:0x7ffb5550a7b0>)

在mayknow的帮助下对其进行编辑后,我想到了这个课程。它的mainButton没有任何onclick动作。

class DropDownMenu: UIStackView {
    
    var options: [String]! = [] // Labels for all of the options
    var titleButton: UIButton! = UIButton() // The Main Title Button
    
    init(options: [String]) {
        self.options = options
        let mainFrame = titleButton.frame
        
        super.init(frame: CGRect(x: mainFrame.minX, y: mainFrame.maxY, width: mainFrame.width, height: mainFrame.height * CGFloat(options.count)))
        
        var y: CGFloat = 0
        for title in self.options {
            let button = UIButton(frame: CGRect(x: 0, y: y, width: self.frame.width, height: mainFrame.height))
            button.setTitle(title, for: .normal)
            button.setTitleColor(titleButton.titleLabel?.textColor, for: .normal)
            button.backgroundColor = titleButton.backgroundColor
            button.addTarget(self, action: #selector(dropDownOptionClicked(_:)), for: .touchUpInside)
            
            button.isHidden = true
            button.alpha = 0
            
            self.addArrangedSubview(button)
            
            y += 1
        }
    }
    
    @objc func openDropDown(_ sender: UIButton) {
        print("Open DropDownMenu")
        for button in self.arrangedSubviews {
            UIView.animate(withDuration: 0.7) {
                button.isHidden = !button.isHidden
                button.alpha = button.alpha == 0 ? 1 : 0
                self.layoutIfNeeded()
                self.superview?.layoutIfNeeded()
            }
        }
    }
    
    @objc private func dropDownOptionClicked(_ sender: UIButton) {
        print(sender.titleLabel?.text as Any)
    }
    
    init() {
        super.init(frame: CGRect.zero)
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

而且比我的ViewController更...

let dp = DropDownMenu(options: ["Label 1", "Label 2", "Label 3"])
        
let titleButton = UIButton(frame: CGRect(x: 100, y: 300, width: 180, height: 40))
titleButton.backgroundColor = UIColor.white
titleButton.setTitle("DropDownMenu", for: .normal)
titleButton.setTitleColor(UIColor.black, for: .normal)
titleButton.layer.borderWidth = 2
titleButton.addTarget(self, action: #selector(dp.openDropDown(_:)), for: .touchUpInside)
        
dp.titleButton = titleButton

错误 ...

按钮目标?:[AnyHashable(<HomeworkHelp.DropDownMenu:0x7ffb555200b0; frame =(0 0; 0 0); layer = <CALayer:0x600002bdf5c0 >>)],self.target:可选(<HomeworkHelp.CreateAccountViewController:0x7ffb5550a7b0>)

仍然出现,我不知道为什么。

亚历克斯·米纳斯扬

我终于想通了!目标必须是DropDownMenu。

titleButton.addTarget(dp, action: #selector(dp.openDropDown(_:)), for: .touchUpInside)

这是其余的代码...

let titleButton = UIButton(frame: CGRect(x: 50, y: 290, width: 100, height: 40))
titleButton.backgroundColor = UIColor.white
titleButton.setTitle("Grade", for: .normal)
titleButton.setTitleColor(UIColor.black, for: .normal)
titleButton.layer.borderWidth = 2
titleButton.layer.cornerRadius = 10
        
let dp = DropDownMenu(options: ["1", "Freshman", "Sophomore", "Junior", "Senior", "College"])
dp.titleButton = titleButton
dp.target = self
dp.borderWidth = 2
dp.spacing = 5
dp.cornerRadius = 10
dp.bgColor = UIColor.white

将其添加到子视图并创建...

view.addSubview(titleButton)
view.addSubview(dp)
dp.createDropDownMenu()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

addTarget:action:forControlEvents:无法识别的选择器发送到实例-uitablecell中的uibutton

来自分类Dev

-[UIView setAdUnitID1:]:无法识别的选择器已发送到实例

来自分类Dev

PKReveal Controller UIVIEW无法识别的选择器已发送到实例

来自分类Dev

“无法识别的选择器已发送到实例”-添加搜索栏时出错

来自分类Dev

iOS无法识别的选择器已发送到Swift中的实例

来自分类Dev

NSNotification无法识别的选择器已发送到Swift中的实例

来自分类Dev

无法识别的选择器已发送到UIButton中的实例

来自分类Dev

NSNotification无法识别的选择器已发送到Swift中的实例

来自分类Dev

无法识别的选择器已发送到MapView iof OS中的实例

来自分类Dev

iOS本机代码中的“无法识别的选择器已发送到实例”错误

来自分类Dev

Swift选择器-无法识别的选择器已发送到实例

来自分类Dev

IOS:无法识别的选择器发送到目标 C 中的实例 Swift 类

来自分类Dev

无法识别的选择器已发送到FBSDKProfile类别的实例

来自分类Dev

-[UIView setContentSize:]:无法识别的选择器已发送到实例0x16d3be60

来自分类Dev

出现UIActivityViewController时,iOS 9崩溃:[UIView _fromWindowOrientation]无法识别的选择器发送到实例(UIView)

来自分类Dev

无法解决“无法识别的选择器已发送到实例”错误

来自分类Dev

无法解决错误“ [__NSCFBoolean长度]:无法识别的选择器已发送到实例”

来自分类Dev

UIView setImage:]:无法识别的选择器发送到实例

来自分类Dev

[UIView setTextFont:]:无法识别的选择器迅速发送到实例

来自分类Dev

[UIView setText:]: 无法识别的选择器发送到实例

来自分类Dev

'NSInvalidArgumentException',原因:'-[UIView setPDFPage:]:集成ZoomingPDFViewer时将无法识别的选择器发送到实例

来自分类Dev

iCarousel-numberOfItemsInCarousel:无法识别的选择器已发送到实例

来自分类Dev

无法识别的选择器已发送到实例。NSInvalidArgumentExpection

来自分类Dev

iOS的FBSDK:“无法识别的选择器已发送到实例”

来自分类Dev

[CBAutocompleteTextFieldField完成:]:无法识别的选择器已发送到实例?

来自分类Dev

UITapGestureRecognizer无法识别的选择器已发送到实例

来自分类Dev

[__NSCFNumber长度]:无法识别的选择器已发送到实例UITableView

来自分类Dev

[__NSCFString objectForKey:]:无法识别的选择器已发送到实例

来自分类Dev

[图像计数]:无法识别的选择器已发送到实例

Related 相关文章

  1. 1

    addTarget:action:forControlEvents:无法识别的选择器发送到实例-uitablecell中的uibutton

  2. 2

    -[UIView setAdUnitID1:]:无法识别的选择器已发送到实例

  3. 3

    PKReveal Controller UIVIEW无法识别的选择器已发送到实例

  4. 4

    “无法识别的选择器已发送到实例”-添加搜索栏时出错

  5. 5

    iOS无法识别的选择器已发送到Swift中的实例

  6. 6

    NSNotification无法识别的选择器已发送到Swift中的实例

  7. 7

    无法识别的选择器已发送到UIButton中的实例

  8. 8

    NSNotification无法识别的选择器已发送到Swift中的实例

  9. 9

    无法识别的选择器已发送到MapView iof OS中的实例

  10. 10

    iOS本机代码中的“无法识别的选择器已发送到实例”错误

  11. 11

    Swift选择器-无法识别的选择器已发送到实例

  12. 12

    IOS:无法识别的选择器发送到目标 C 中的实例 Swift 类

  13. 13

    无法识别的选择器已发送到FBSDKProfile类别的实例

  14. 14

    -[UIView setContentSize:]:无法识别的选择器已发送到实例0x16d3be60

  15. 15

    出现UIActivityViewController时,iOS 9崩溃:[UIView _fromWindowOrientation]无法识别的选择器发送到实例(UIView)

  16. 16

    无法解决“无法识别的选择器已发送到实例”错误

  17. 17

    无法解决错误“ [__NSCFBoolean长度]:无法识别的选择器已发送到实例”

  18. 18

    UIView setImage:]:无法识别的选择器发送到实例

  19. 19

    [UIView setTextFont:]:无法识别的选择器迅速发送到实例

  20. 20

    [UIView setText:]: 无法识别的选择器发送到实例

  21. 21

    'NSInvalidArgumentException',原因:'-[UIView setPDFPage:]:集成ZoomingPDFViewer时将无法识别的选择器发送到实例

  22. 22

    iCarousel-numberOfItemsInCarousel:无法识别的选择器已发送到实例

  23. 23

    无法识别的选择器已发送到实例。NSInvalidArgumentExpection

  24. 24

    iOS的FBSDK:“无法识别的选择器已发送到实例”

  25. 25

    [CBAutocompleteTextFieldField完成:]:无法识别的选择器已发送到实例?

  26. 26

    UITapGestureRecognizer无法识别的选择器已发送到实例

  27. 27

    [__NSCFNumber长度]:无法识别的选择器已发送到实例UITableView

  28. 28

    [__NSCFString objectForKey:]:无法识别的选择器已发送到实例

  29. 29

    [图像计数]:无法识别的选择器已发送到实例

热门标签

归档