[iOS/Error] unrecognized selector sent to class

yeonjiyoooยท2025๋…„ 2์›” 25์ผ
0

iOS

๋ชฉ๋ก ๋ณด๊ธฐ
2/2

๐Ÿšซ ์˜ค๋ฅ˜ ๋‚ด์šฉ

Exception	NSException *	"+[TapAndAlarm.SubjectTimePicker timeButtonTapped]: unrecognized selector sent to class 0x102861c38"	0x0000600000c84630

unrecognized selector sent to class ~

์ด ์˜ค๋ฅ˜๋Š” 'timeButtonTapped' ๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ 'TapAndAlarm.SubjectTimePicker'๋ผ๋Š” ํด๋ž˜์Šค์—์„œ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋Š” ๋œป์ด๋‹ค.

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์›์ธ์„ ์•Œ์•„๋ณด์ž!


class SubjectTimePicker: UIView  {
   
    private let timeButton: UIButton = {
        let button = UIButton()
        button.setTitle("35๋ถ„", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 24, weight: .semibold)
        button.layer.cornerRadius = 8
        //์˜ค๋ฅ˜ ๋ฐœ์ƒ ๊ตฌ๊ฐ„
        button.addTarget(SubjectTimePicker.self, action: #selector(timeButtonTapped), for: .touchUpInside)
        //์˜ค๋ฅ˜ ๋ฐœ์ƒ ๊ตฌ๊ฐ„
        return button
    }()
   
 	@objc private func timeButtonTapped() {
         pickerView.isHidden.toggle()
	}
       
}

๐Ÿค” ์˜ค๋ฅ˜ ์›์ธ

swift์—์„œ addTarget์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ ๋˜์–ด์•ผ ํ•œ๋‹ค. ํ•ด๋‹น ์ธ์Šคํ„ด์Šค์—์„œ #selector(_) ์— ๋ช…์‹œ๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐพ๊ณ  ์‹คํ–‰์‹œํ‚จ๋‹ค.

ํ•˜์ง€๋งŒ ์˜ค๋ฅ˜ ์ฝ”๋“œ์—์„œ addTarget์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” SubjectTimePicker.self๋กœ ํด๋ž˜์Šค์ด๋‹ค. ์‹คํ–‰์‹œํ‚ค๊ณ ์ž ํ•˜๋Š” timeButtonTapped๋Š” ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ๋กœ ํด๋ž˜์Šค์—์„œ ์ง์ ‘ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐพ์ง€ ๋ชปํ•˜๊ณ  unrecognized selector ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๐Ÿ› ๏ธ ์˜ค๋ฅ˜ ํ•ด๊ฒฐ

addTarget์˜ ์ธ์ž ์„ค์ •์— ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋˜ ๊ฒƒ์ด๋ฏ€๋กœ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ์ธ์Šคํ„ด์Šค๋ฅผ ๋œปํ•˜๋Š” self๋กœ ๊ณ ์ณ์ฃผ๋ฉด ๋œ๋‹ค!

button.addTarget(self, action: #selector(timeButtonTapped), for: .touchUpInside)
profile
1 ^ 365 = 1 BUT 1.01 ^ 365 = 37์ฉœ ์–ด์ฉŒ๊ณ ...

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด