ํจ์
UI ์ฝ๋๋ฅผ viewDidLoad์ ์์ฑ์ ํ๋ค๋ณด๋ฉด,
ํ๋์ ๋ณด๊ธฐ๋ ํ๋ค๊ณ , ์ถํ์ ์ ์ง๋ณด์๋ฅผ ํ๊ธฐ๋ ์ข์ง ์์ ๊ฒ๋๋ค.
๊ทธ๋์ ๊ฐ ๊ธฐ๋ฅ๋ณ๋ก ํจ์๋ฅผ ๋ง๋ค์ด์ viewDidLoad์ ํธ์ถํ๋ ๋ฐฉ์์ผ๋ก ์ ๋ฆฌํ์ฃ .
์ฌ๊ธฐ์ ์๊ธฐ๋ ๊ถ๊ธ์ฆ!๐ค
'์ฐ๋ฆฌ๊ฐ ์ง์ ๋ง๋ designButtonUI๋ ํธ์ถ์ ํด์ฃผ๋ฉด์..
์ viewDidLoad ํจ์๋ Action ํจ์๋ ํธ์ถ์ ํ์ง ์์๋ ๋๋๊ฑฐ์ง??'
์ด ๋ถ๋ถ์ ๋ํด์ ํ ๋ฒ ์ ๋ฆฌํด๋ณด๋ ค๊ณ ํฉ๋๋ค!!๐
class ViewController: UIViewController {
@IBOutlet var button1: UIButton!
@IBOutlet var button2: UIButton!
@IBOutlet var button3: UIButton!
@IBOutlet var label1: UILabel!
@IBOutlet var label2: UILabel!
@IBOutlet var label3: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
designLabelUI(label1, thisIsTextColor: .brown)
designLabelUI(label2, thisIsTextColor: .systemPink)
designLabelUI(label3, thisIsTextColor: .blue)
designButtonUI()
}
func designLabelUI(_ a: UILabel, thisIsTextColor color: UIColor) {
a.text = "0"
a.textColor = color
a.font = .systemFont(ofSize: 30)
a.textAlignment = .center
}
func designButtonUI() {
button1.setTitle("๋ฒํผ 1", for: .normal)
button1.tintColor = .black
button2.setTitle("๋ฒํผ 2", for: .normal)
button2.tintColor = .black
button3.setTitle("๋ฒํผ 3", for: .normal)
button3.tintColor = .black
}
@IBAction func button1Clicked(_ sender: UIButton) {
label1.text = "ํด๋ฆญ๋จ!"
}
}
์ ํ์ด ๋ง๋ ํจ์ & ์ฌ์ฉ์๊ฐ ์ง์ ๋ง๋ ํจ์
๋ ํจ์์ ์ฐจ์ด์ ์ ๋ฌด์์ผ๊น์?
์ฝ๋๋ฅผ ์กฐ๊ธ ๋ ๊น๋ํ๊ฒ, ์ ์ง๋ณด์ํ๊ธฐ ์ฝ๊ฒ ๋ง๋ค๊ธฐ ์ํด์ ์ง์ ์์ฑํ๋ ํจ์๋
์ฐ๋ฆฌ๊ฐ ์ง์ ์ด๋ฆ๋ ์ง์ด์ฃผ๊ณ , ๊ธฐ๋ฅ๋ ์ ๊ณ , viewDidLoad์ ํธ์ถํจ์ผ๋ก์ ์คํ ํ์ด๋ฐ๋ ์ ํด์ฃผ์ด์ผ ํฉ๋๋ค.
์ด๋ฅผ ์ฌ์ฉ์๊ฐ ์ง์ ์ ์ํด์ฃผ๋ ํจ์๋ผ๋ ๋ป์ผ๋ก 'UDF'(User Defined Function)์ด๋ผ๊ณ ํฉ๋๋ค.
ํ์ง๋ง, viewDidLoad๋ IBAction๊ณผ ๊ฐ์ ํจ์๋ค์
์ ํ์ด ๋ง๋ค์ด ๋์ ํจ์์ด๊ธฐ ๋๋ฌธ์ ๊ธฐ๋ฅ์ด๋ ์คํ ํ์ด๋ฐ์ ๋ํด์๋ ์ด๋ฏธ ์ ์๋์ด ์์ต๋๋ค.
๊ทธ๋์ ์ฐ๋ฆฌ๊ฐ ์ ํด์ค ํ์๊ฐ ์์ฃ .
๊ทธ๋ผ ์ฌ๊ธฐ์ ์๊ธฐ๋ ๊ถ๊ธ์ฆ ํ๋ ๋!!๐ง
'์? ์ฐ๋ฆฌ IBAction ์ฐ๊ฒฐํด์ค ๋, ์ด๋ฆ ์จ์ฃผ์์..?'
์ด์ ๋ํด์๋ ์๋ ์ฌ์ง์ผ๋ก ์ค๋ช
ํ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
Event๋ฅผ ํตํด์ ์คํ ํ์ด๋ฐ์ ์ ํด์ฃผ๋ ๊ฒ์ด์ฃ .
๋งค๊ฐ๋ณ์
override func viewDidLoad() {
super.viewDidLoad()
designLabelUI()
}
func designLabelUI() {
label1.text = "0"
label1.textColor = color
label1.font = .systemFont(ofSize: 20)
label1.textAlignment = .center
}
๋งค๊ฐ๋ณ์๋ฅผ ํ์ฉํด์ ์ฝ๋๋ฅผ ์์ฑํด๋ด
์๋ค.
override func viewDidLoad() {
super.viewDidLoad()
designLabelUI(label1, thisIsTextColor: .brown)
designLabelUI(label2, thisIsTextColor: .systemPink)
designLabelUI(label3, thisIsTextColor: .blue)
}
func designLabelUI(_ a: UILabel, thisIsTextColor color: UIColor) {
a.text = "0"
a.textColor = color
a.font = .systemFont(ofSize: 20)
a.textAlignment = .center
}
์ฉ์ด๋ฅผ ์ ๋ฆฌํด๋ณด์๋ฉด
// labelName: ๋งค๊ฐ๋ณ์(Parameter)
// label1, label2, label3: ์ ๋ฌ์ธ์(Argument)
// ์ธ๋ถ ๋งค๊ฐ๋ณ์(Argument Label) ex. thisIsTextColor
// ๋ด๋ถ ๋งค๊ฐ๋ณ์(Parameter Name) ex. a, color
// _: ์์ผ๋์นด๋์๋ณ์ <- ๋ฐ์์ ๊ตณ์ด ์ธ๊ธํ์ง ์์๋ ๋ ๊ฒ ๊ฐ์ ๋!
์ด๋ ๊ฒ ๋ถ๋ฆ
๋๋ค!!
override func viewDidLoad() {
super.viewDidLoad()
designButtonUI()
}
func designButtonUI(_ button: UIButton, title: String, titleColor: UIColor) {
button1.setTitle(title, for: .normal)
button1.tintColor = titleColor
button2.setTitle(title, for: .normal)
button2.tintColor = titleColor
button3.setTitle(title, for: .normal)
button3.tintColor = titleColor
}
์ด๋ฒ์๋ ๋ฒํผ์ ์ฐ๊ฒฐํ๋ค๊ณ ๊ฐ์ ํ๊ณ ,
๋งค๊ฐ ๋ณ์๋ฅผ ํ์ฉํ์ฌ ์ฝ๋๋ฅผ ์ค์ฌ๋ณด๊ฒ ์ต๋๋ค.
override func viewDidLoad() {
super.viewDidLoad()
designButtonUI(button1, title: "๋ฒํผ1", titleColor: .brown)
designButtonUI(button2, title: "๋ฒํผ2", titleColor: .systemPink)
designButtonUI(button3, title: "๋ฒํผ3", titleColor: .blue)
}
func designButtonUI(_ button: UIButton, title: String, titleColor: UIColor) {
button.setTitle(title, for: .normal)
button.tintColor = titleColor
}
์ฒ์์๋ ์ด์ํ๊ณ , ๋ ๋ณด๊ธฐ ์ด๋ ค์ด ๊ฒ ๊ฐ๋ค๋ ์๊ฐ์ด ๋ค ์ ์์ต๋๋ค.
ํ์ง๋ง! ๊ณ์ ์ฐ๋ค๋ณด๋ฉด ํจ์ฌ ๋ฐ๋ณต๋๋ ์ฝ๋๊ฐ ์ค์ด๋ค๊ณ ,
๊ฐ๋
์ฑ์ด ์ข๋ค๋ ๊ฑธ ๋๋ ์ ์์ ๊ฑฐ๋ผ๋ ์๊ฐ์ด ๋ญ๋๋ค!๐
'Swift > Xcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] View Drawing Cycle (0) | 2024.05.28 |
---|---|
[Swift] Delegate ํจํด (0) | 2024.05.26 |
[Swift] UserDefaults (0) | 2024.05.25 |
[Swift] tag ํ์ฉํ์ฌ ์ฝ๋ ๊ฐ์ํํ๊ธฐ (0) | 2024.05.20 |
[Swift] Storyboard ์ฌ์ฉ ์ Outlet ์ด๋ฆ ๋ณ๊ฒฝ ๋ฐฉ๋ฒ (0) | 2024.05.16 |