addTarget 호출시 UIView 클래스의 "인식되지 않은 선택기가 인스턴스로 전송 됨"

Alex_Minasyan

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 () 함수의 첫 번째 print 문은 다음을 인쇄합니다.

버튼 대상? : [AnyHashable (<HomeworkHelp.DropDownMenu : 0x7ffb555200b0; frame = (0 0; 0 0); layer = <CALayer : 0x600002bdf5c0 >>)], self.target : Optional (<HomeworkHelp.CreateAccountViewController : 0x7ffb5550a7b0>)

mightknow의 도움으로 그것을 편집 한 후에 나는이 수업을 생각 해냈다. 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 : Optional (<HomeworkHelp.CreateAccountViewController : 0x7ffb5550a7b0>)

여전히 나오고 왜 그런지 모르겠습니다.

Alex_Minasyan

나는 마침내 그것을 알아 냈습니다! 대상은 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

저장 버튼 클릭 결과 "인식되지 않은 선택기가 인스턴스로 전송 됨"

분류에서Dev

iOS 기본 코드의 "인식되지 않은 선택기가 인스턴스로 전송 됨"오류

분류에서Dev

클래스 메서드에서 addTarget : action : forControlEvents :를 호출하는 방법은 무엇입니까?

분류에서Dev

루프 끝에서 호출되는 UIView 클래스

분류에서Dev

UIScrollview에 호출 및 추가 될 때 사용자 정의 UIView 클래스의 속성이 설정되지 않음

분류에서Dev

iOS 8의 하위 클래스 UIView에서 touchesBegan을 호출하는 방법은 무엇입니까?

분류에서Dev

UIView 클래스 내에서 fromRootViewController 메서드를 어떻게 호출합니까?

분류에서Dev

클래스의 한 인스턴스에서 UIView beginAnimations가 동일한 클래스의 다른 인스턴스에서 beginAnimations를 발생시킵니다.

분류에서Dev

UIView의 하위 클래스 만들기

분류에서Dev

Delphi XE5로 UIView 클래스 [Swizzling] 내부에서 시작

분류에서Dev

내비게이션 바 버튼 클릭시 UIview에 정의 된 메소드 호출 방법

분류에서Dev

사용자 정의 UIView 클래스는 초기화시 항상 충돌합니다.

분류에서Dev

다른 클래스에서 변경이 호출 될 때 UIView IBOutlet이 변경되지 않음

분류에서Dev

UIView 하위 클래스에서 UIViewController를 호출하는 방법은 무엇입니까?

분류에서Dev

createLabel 호출시 클래스 설정

분류에서Dev

PHP 클래스 호출 시도 오류

분류에서Dev

객체 클래스의 메서드 호출시 attributeError

분류에서Dev

수퍼 클래스의 __init__ 메서드 호출시 TypeError

분류에서Dev

템플릿 클래스의 친구 함수 호출 시도

분류에서Dev

extJS 클래스의 initComponent를 명시 적으로 호출

분류에서Dev

신호 및 슬롯 시스템에 의한 호출 클래스 기능

분류에서Dev

자바의 클래스를 호출?

분류에서Dev

SQLiteOpenHelper 클래스의 호출 활동

분류에서Dev

클래스의 다른 구현 호출

분류에서Dev

다른 클래스의 함수 호출

분류에서Dev

다른 클래스의 한 클래스에서 함수 호출

분류에서Dev

UIView + Category 액세스 dealloc 또는 UIView dealloc 호출에 대한 작업 수행

분류에서Dev

호출 함수 오류 : 클래스의 명시 적 인스턴스

분류에서Dev

클래스 변수 호출시 메시지 출력

Related 관련 기사

  1. 1

    저장 버튼 클릭 결과 "인식되지 않은 선택기가 인스턴스로 전송 됨"

  2. 2

    iOS 기본 코드의 "인식되지 않은 선택기가 인스턴스로 전송 됨"오류

  3. 3

    클래스 메서드에서 addTarget : action : forControlEvents :를 호출하는 방법은 무엇입니까?

  4. 4

    루프 끝에서 호출되는 UIView 클래스

  5. 5

    UIScrollview에 호출 및 추가 될 때 사용자 정의 UIView 클래스의 속성이 설정되지 않음

  6. 6

    iOS 8의 하위 클래스 UIView에서 touchesBegan을 호출하는 방법은 무엇입니까?

  7. 7

    UIView 클래스 내에서 fromRootViewController 메서드를 어떻게 호출합니까?

  8. 8

    클래스의 한 인스턴스에서 UIView beginAnimations가 동일한 클래스의 다른 인스턴스에서 beginAnimations를 발생시킵니다.

  9. 9

    UIView의 하위 클래스 만들기

  10. 10

    Delphi XE5로 UIView 클래스 [Swizzling] 내부에서 시작

  11. 11

    내비게이션 바 버튼 클릭시 UIview에 정의 된 메소드 호출 방법

  12. 12

    사용자 정의 UIView 클래스는 초기화시 항상 충돌합니다.

  13. 13

    다른 클래스에서 변경이 호출 될 때 UIView IBOutlet이 변경되지 않음

  14. 14

    UIView 하위 클래스에서 UIViewController를 호출하는 방법은 무엇입니까?

  15. 15

    createLabel 호출시 클래스 설정

  16. 16

    PHP 클래스 호출 시도 오류

  17. 17

    객체 클래스의 메서드 호출시 attributeError

  18. 18

    수퍼 클래스의 __init__ 메서드 호출시 TypeError

  19. 19

    템플릿 클래스의 친구 함수 호출 시도

  20. 20

    extJS 클래스의 initComponent를 명시 적으로 호출

  21. 21

    신호 및 슬롯 시스템에 의한 호출 클래스 기능

  22. 22

    자바의 클래스를 호출?

  23. 23

    SQLiteOpenHelper 클래스의 호출 활동

  24. 24

    클래스의 다른 구현 호출

  25. 25

    다른 클래스의 함수 호출

  26. 26

    다른 클래스의 한 클래스에서 함수 호출

  27. 27

    UIView + Category 액세스 dealloc 또는 UIView dealloc 호출에 대한 작업 수행

  28. 28

    호출 함수 오류 : 클래스의 명시 적 인스턴스

  29. 29

    클래스 변수 호출시 메시지 출력

뜨겁다태그

보관