→
์ด๋ฒ ์ฝ๋๋ฒ ์ด์ค๋ก ๊ตฌํํ ๋ ๊ฐ์ฅ ์ด๋ ค์ ๋ ๋ถ๋ถ์ด
๊ฐ ์ฝ๋๋ค๊ฐ์ ์ฐ๊ฒฐ๊ณ ๋ฆฌ๋ฅผ ์ด์ด๋๊ฐ๋ ๊ฒ์ด ์ด๋ ค์ ๋ค.
๊ทธ๋์ ํ๋ํ๋ ๋ฏ์ด๋ณด๋ฉด์ ๊ณต๋ถํ๋ค ๋ณด๋ ์๋ฌธ์ด ๋๋ ๋ฉ์๋๋ค.
์ด๋ฒคํธ ๋ฐ์ ์ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ๋ง์ด ์ฌ์ฉํ๊ณค ํ๋๋ฐ
target์ ์ฃผ๋ก self๊ฐ ์ฌ์ฉ๋์ด ์๋ก์ ์ผ๋ก ์ฌ์ฉํ๊ณค ํ์๋ค.
ํ์ง๋ง ์ ํํ๊ฒ ๋ฌด์์ ์๋งํ๊ณ ,
์ด๋ป๊ฒ ์ด ์ฝ๋๋ก ์ธํด ํด๋น ๋ฉ์๋๊ฐ ์คํ๋๋์ง์ ๋ํ
์๋ฌธ์ด ์๊ฒจ ํด๋น ๋ด์ฉ์ ์ ๋ฆฌํด๋ณธ๋ค.
์ปจํธ๋กค๊ณผ ํ๊ฒ ๊ฐ์ฒด ๋ฐ ์ก์ ๋ฉ์๋๋ฅผ ์ฐ๊ฒฐํฉ๋๋ค.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 100, width: 200, height: 50)
button.setTitle("Tap me", for: .normal)
// self๋ฅผ target์ผ๋ก ์ค์
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
self.view.addSubview(button)
}
@objc func buttonTapped() {
// ์ฌ๊ธฐ์ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํจ
print("Button tapped!")
// ์: ๋ผ๋ฒจ ์
๋ฐ์ดํธ, ํ๋ฉด ์ ํ ๋ฑ
}
}
์ก์ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋์ ๊ฐ์ฒด์ ๋๋ค. ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋ ์ก์ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค.
self๋ฅผ ์ฌ์ฉํ๋ฉด ํ์ฌ ํด๋์ค(์ฃผ๋ก ๋ทฐ ์ปจํธ๋กค๋ฌ)๊ฐ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๋ค.
→ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋ ํด๋น ์ก์ ๋ฉ์๋๊ฐ ๋ทฐ ์ปจํธ๋กค๋ฌ ํด๋์ค ๋ด๋ถ์์ ์คํ๋๋ค๋ ์๋ฏธ
๊ทธ๋ผ self ๋์ ๋ค๋ฅธ ์ปจํธ๋กค๋ฌ๋ก ์ค์ ํ๋ฉด ํด๋น ์ปจํธ๋กค๋ฌ์์ ์คํ๋๋๊ฐ?
Yes! ์ด๋ฒคํธ๋ฅผ ๋ค๋ฅธ ๋ทฐ ์ปจํธ๋กค๋ฌ์์ ์ฒ๋ฆฌํ๊ณ ์ถ๋ค๋ฉด ๊ทธ ์ปจํธ๋กค๋ฌ๋ฅผ target์ผ๋ก ์ง์ ํ๋ฉด ๋๋ค.
class AnotherViewController: UIViewController {
@objc func buttonTapped() {
print("Button tapped in AnotherViewController!")
}
}
class ViewController: UIViewController {
let anotherVC = AnotherViewController()
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 100, width: 200, height: 50)
button.setTitle("Tap me", for: .normal)
// anotherVC๋ฅผ target์ผ๋ก ์ค์
button.addTarget(anotherVC, action: #selector(anotherVC.buttonTapped), for: .touchUpInside)
self.view.addSubview(button)
}
}
nil์ ์ฌ์ฉํ๋ฉด UIKit์ ๋ฆฌ์คํฐ๋ ์ฒด์ธ์ ๊ฒ์ํด ์ก์ ์ ์ฒ๋ฆฌํ ์ ์ ํ ๊ฐ์ฒด๋ฅผ ์ฐพ๋๋ค.
์ด๋ ๊ฒ ์ค์ ํ๋ฉด ๋ฒํผ์ด ๋๋ ธ์ ๋ ์ง์ ํ ๋ฉ์๋๊ฐ ํธ์ถ๋์ด ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๊ฒ ๋๋ค.
๋ฆฌ์คํฐ๋ ์ฒด์ธ
target์ nil๋ก ์ง์ ํ๋ฉด UIKit์ด ๋ฆฌ์คํฐ๋ ์ฒด์ธ์ ๊ฒ์ํ์ฌ ํด๋น ์ก์ ์ ์ฒ๋ฆฌํ ๊ฐ์ฒด๋ฅผ ์ฐพ์ต๋๋ค.
์ด๋ ์ด๋ฒคํธ๊ฐ ํน์ ๊ฐ์ฒด์ ์ํด ์ฒ๋ฆฌ๋์ง ์์ ๋, ํด๋น ๊ฐ์ฒด์ ๋ถ๋ชจ๋ก ์ด๋ฒคํธ๊ฐ ์ ํ๋๋ ๋ฉ์ปค๋์ฆ์ ๋๋ค.
์ด๋ฌํ ๋ฉ์๋ ์ฌ์ฉ ๋ฐฉ์์ MVC ํจํด์ ์ ํ์ฉํ์ฌ ์ฝ๋ ๊ตฌ์กฐ๋ฅผ ๋ช ํํ๊ฒ ํ๊ณ , ๋ทฐ์ ์ปจํธ๋กค๋ฌ์ ์ญํ ์ ๋ช ํํ ๊ตฌ๋ถํ ์ ์๊ฒ ๋์์ค๋๋ค. self๋ฅผ ์ง์ ํ๋ฉด ์ด๋ฒคํธ ์ฒ๋ฆฌ ๋ก์ง์ด ๋ทฐ ์ปจํธ๋กค๋ฌ ๋ด๋ถ์ ์์ด ๊ด๋ฆฌํ๊ธฐ ์ฌ์ด ๊ตฌ์กฐ๊ฐ ๋ฉ๋๋ค.
ํธ์ถํ ์ก์ ๋ฉ์๋๋ฅผ Selector๋ก ์ง์ ํ๋ค.
์ด๋ค ์ด๋ฒคํธ์ ๋ฐ์ํ ์ง ์ง์ ํ๋ค.
1. ๊ฐ ๋ฒํผ์ ๋์ผํ ํ์ผ๊ณผ ์ก์ ์ ์ค์ ํ๋ค.
2. tag ์์ฑ์ ์ฌ์ฉํด ๊ฐ ๋ฒํผ์ ๊ณ ์ ํ ํ๊ทธ๋ฅผ ์ง์ ํด ์ด๋ค ๋ฒํผ์ด ๋๋ ธ๋์ง ๊ตฌ๋ถํ ์ ์๋ค.
3. ์ก์ ๋ฉ์๋๊ฐ ํธ์ถ๋ ๋, UIButton ์ธ์คํด์ค๊ฐ sender(๋งค๊ฐ๋ณ์: ์ด๋ฒคํธ๋ฅผ ํธ๋ฆฌ๊ฑฐํ ๊ฐ์ฒด. ์ฆ, ๋ฒํผ)๋ก ์ ๋ฌ๋ผ
์ด๋ค ๋ฒํผ์ด ๋๋ ธ๋์ง ์๋ณํ๋ค.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// ์ฒซ ๋ฒ์งธ ๋ฒํผ ์์ฑ
let button1 = UIButton(type: .system)
button1.frame = CGRect(x: 100, y: 100, width: 200, height: 50)
button1.setTitle("Button 1", for: .normal)
button1.tag = 1
button1.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
self.view.addSubview(button1)
// ๋ ๋ฒ์งธ ๋ฒํผ ์์ฑ
let button2 = UIButton(type: .system)
button2.frame = CGRect(x: 100, y: 200, width: 200, height: 50)
button2.setTitle("Button 2", for: .normal)
button2.tag = 2
button2.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
self.view.addSubview(button2)
}
@objc func buttonTapped(_ sender: UIButton) {
if sender.tag == 1 {
print("Button 1 tapped!")
} else if sender.tag == 2 {
print("Button 2 tapped!")
}
}
}
[UIKit] UITableView ์ฝ๋๋ฒ ์ด์ค ์ฌ์ฉ๋ฒ (0) | 2024.07.04 |
---|---|
[UIKit] ํํ๋ชจ๋ฌ ๊ตฌํ(Scrollable Bottom Sheet) - SheetPresentationController (0) | 2024.07.03 |
ํท๊ฐ๋ฆฌ๋ UIKit ์์ฑ๊ณผ SnapKit ์ ์ฝ ์์ฑ (0) | 2024.06.27 |
์คํ ๋ฆฌ๋ณด๋๋ก UIScrollView ๋ง๋ค๊ธฐ (0) | 2024.06.22 |
UIStackView (0) | 2024.06.21 |