Delegate Pattern
Delegate ํจํด์ ๋์์ธ ํจํด ์ค ํ๋๋ก
๋จ์ด ๋ป ๊ทธ๋๋ก '์์ํ๋ค'์ ๊ฐ์ด ํ๋์ ๊ฐ์ฒด๊ฐ ๋ชจ๋ ์ผ์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์๋
๊ฐ์ฒด๊ฐ ๋ค๋ฅธ ๊ฐ์ฒด์๊ฒ ์์ ์ ์ผ์ ์์ํ๋ ํํ์ ๋์์ธ ํจํด์
๋๋ค.
<์ด ํจํด์ ์ฅ์ >
1. ํ๋กํ ์ฝ์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ์ฌ์ฌ์ฉ์ฑ์ด ๋ฐ์ด๋๋ค.
2. ํ๋กํ ์ฝ์ ํ์ํ ๋ฉ์๋๋ค์ด ๋ช
์๋์ด ์ ๋ฌ๋๋ค.
3. ์ฒ๋ฆฌํด์ผํ๋ ์ผ์ ์ผ๋ถ๋ฅผ ๋ค๋ฅธ ๊ฐ์ฒด์๊ฒ ์์ํ ์ ์๋ค.
์ง๊ธ๊น์ง ์ฑ์ ๋ง๋ค๋ฉด์, ์๊ฒ ๋ชจ๋ฅด๊ฒ ์ฌ๋ฌ Delegate ํ๋กํ ์ฝ์ด ์์์ ๊ฒ๋๋ค.
(UITableViewDelegate, UITextFieldDelegate, ๋ฑ๋ฑ..)
tableViewController๋ command๋ก ๋๋ฌ๋ณด๋ฉด,
UITableViewDelegate๋ฅผ ์ฑํํ์ฌ ํ๋กํ ์ฝ์ ์ค์ํ๊ณ ์์์ ์ ์ ์์ต๋๋ค.
์ด๋ฌํ Delegate ํ๋กํ ์ฝ ๋ฉ์๋๋ค์
์ด๋ฏธ ์ ํ์์ ๋ง๋ค์ด ๋์๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฐ์๋ค์ ๋ชฉ์ ์ ๋ง๊ฒ ์ ํํด์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
์ ํ์ด ์ง์ ๋ง๋ Delegate ํ๋กํ ์ฝ ๋ง๊ณ
๊ฐ๋ฐ์๊ฐ ์ง์ Customํด์ ๊ตฌํํ ์๋ ์์ต๋๋ค.
์ง์ ํ๋กํ ์ฝ์ ๊ตฌํํ ํ, ๋์์ ์ ์ํ๊ณ ์ํ๋ ๊ฐ์ฒด์ ์์ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
1. Delegate ํ๋กํ ์ฝ ์์ฑ
2. ๋ฉ์๋ ์ ์
3. ์์์ ์ค์
์ ๊ฐ Delegate ํจํด์ ์ฒ์ ์ ํ๊ฒ ๋ ์ผ์ด์ค๋
tableView๋ฅผ ์ฌ์ฉํด์ todoList๋ฅผ ๋ง๋๋๋ฐ, ๋ ๊ฐ์ Cell๊ณผ์ ์ํธ์์ฉ์ด ํ์ํ๋ ์์ ์ด์์ต๋๋ค.
A Cell์์ ์ถ๊ฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด,
B Cell์ ํด๋น ๋ด์ฉ์ด ์ถ๊ฐ๋๋๋ก ๊ตฌํํด์ผ ํ์ต๋๋ค.
ํ์ง๋ง, ๊ฐ Cell ํ์ผ๋ง๋ค ๊ฐ๊ฐ ๊ตฌ์ฑ๋์ด ์๋ ์์๋ค์ Outlet ์ฐ๊ฒฐ์ ํ๊ธฐ ๋๋ฌธ์
์ถ๊ฐ ๋ฒํผ์ Action์ ์ฐ๊ฒฐํ์ ๋,
๋ค๋ฅธ Cell์ด ๊ฐ์ง๊ณ ์๋ ์์์ ์ ๊ทผํ ์ ์์ด์
์ํ๋ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์์ฃ ...๐คฏ
ํ์์ ํ๋์ Cell ํ์ผ๋ก ์์
ํ๋ ๊ฒฝํ๋ง ์์๊ธฐ ๋๋ฌธ์
์ด๋ฐ ๊ฒฝ์ฐ๋ ์ด๋ป๊ฒ ํด์ผํ๋ ๋ฉ๋ถ์ด ์์์ต๋๋ค.
๊ทธ๋, ๊ตฌ๊ธ๋ง์ ํด๋ณด๋ Delegate ํจํด์ ์ฌ์ฉํด์ ํด๊ฒฐํ ์ ์๋ค๋ ์ฌ์ค์ ์๊ฒ๋์์ต๋๋ค.
์ ๊ฐ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํด๋ณด๋ฉด,
1. Delegate ํ๋กํ ์ฝ ์์ฑ
2. Cell ํ์ผ์์ ํ๋กํ ์ฝ์ ์ ์ฅํ ์ ์๋ ์ ์ฅ ํ๋กํผํฐ ์์ฑ
3. ๋ทฐ์ปจ์์ 1๋ฒ์์ ์์ฑํ ํ๋กํ ์ฝ์ ์ฑํํ๊ณ , ํ์ํ ๋ฉ์๋ ๊ตฌํ
4. ๋ทฐ์ปจ์ cellForRowAt์์ cell์ ์ ์ฅ ํ๋กํผํฐ delegate๋ฅผ self๋ก ํ ๋น
5. cell ๋ด๋ถ์์ ๋ฒํผ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด, ์ด delegate ํ๋กํผํฐ์ ์ ๊ทผํ์ฌ ๋ฉ์๋ ํธ์ถ
< 1 - VC >
protocol AddButtonDelegate: AnyObject {
func addButtonClicked(textField: UITextField)
}
< 2 - Cell >
class TextFieldTableViewCell: UITableViewCell {
static let identifier = "textFieldCell"
weak var delegate: AddButtonDelegate? // ์ ์ฅ ํ๋กํผํฐ ์์ฑ
@IBOutlet var inputTodoTextField: UITextField!
@IBOutlet var addButton: UIButton!
}
< 3 - VC >
extension TodoListTableViewController: AddButtonDelegate {
func addButtonClicked(textField: UITextField) {
// ํ์ํ ์๊ตฌ์ฌํญ
}
}
< 4 - VC: cellForRowAt >
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: TextFieldTableViewCell.identifier, for: indexPath) as? TextFieldTableViewCell else { return UITableViewCell() }
cell.delegate = self // self๋ก ํ ๋น
// ๊ธฐํ ๊ตฌํ ์ฝ๋
return cell
}
}
< 5 - Cell >
class TextFieldTableViewCell: UITableViewCell {
static let identifier = "textFieldCell"
weak var delegate: AddButtonDelegate?
@IBOutlet var inputTodoTextField: UITextField!
@IBOutlet var addButton: UIButton!
@IBAction func addButtonClicked(_ sender: UIButton) {
delegate?.addButtonClicked(textField: inputTodoTextField)
// ์ด๋ฒคํธ ๋ฐ์ Action์์ Delegate ํ๋กํผํฐ์ ์ ๊ทผ -> ๋ฉ์๋ ํธ์ถ
}
}
์ด๋ ๊ฒ ์ฝ๋๋ฅผ ์์ฑํ์ต๋๋ค.
Delegate ํจํด์ ํตํด์ Cell์์ ํ์ํ ์์ ์ ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์๊ฒ ๋์๊ณ ,
VC์์ ํ์ํ ๋ถ๋ถ๋ค์ ์์๋ฐ์ ์ํํ๊ฒ ๋ง๋ฌ์ผ๋ก์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ณด๋ค ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๊ฒ ๋์ต๋๋ค. โบ๏ธ
๋ ์์ธํ ์ ๋ฆฌ๋
์ถํ์ ์ถ๊ฐ์ ์ผ๋ก ์์ฑํด๋ณด๊ฒ ์ต๋๋ค!
๐ํผ๋๋ฐฑ์ด ์์ผ์๋ค๋ฉด, ์ธ์ ๋ ์ง ๋๊ธ๋ก ์๋ ค์ฃผ์ธ์!๐
'Swift > Xcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] UIButton Configuration (0) | 2024.05.30 |
---|---|
[Swift] View Drawing Cycle (0) | 2024.05.28 |
[Swift] UserDefaults (0) | 2024.05.25 |
[Swift] tag ํ์ฉํ์ฌ ์ฝ๋ ๊ฐ์ํํ๊ธฐ (0) | 2024.05.20 |
[Swift] ํจ์์ ๋งค๊ฐ๋ณ์๋ฅผ ํ์ฉํด์ ์ฝ๋ ๊ฐ์ ํ๊ธฐ (0) | 2024.05.20 |