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)