Swift/Xcode

[Swift] ํ•จ์ˆ˜์™€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•ด์„œ ์ฝ”๋“œ ๊ฐœ์„ ํ•˜๊ธฐ

yeggrrr๐Ÿผ 2024. 5. 20. 13:16

ํ•จ์ˆ˜


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
        
    }



์ฒ˜์Œ์—๋Š” ์–ด์ƒ‰ํ•˜๊ณ , ๋” ๋ณด๊ธฐ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


ํ•˜์ง€๋งŒ! ๊ณ„์† ์“ฐ๋‹ค๋ณด๋ฉด ํ›จ์”ฌ ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ์ค„์–ด๋“ค๊ณ ,
๊ฐ€๋…์„ฑ์ด ์ข‹๋‹ค๋Š” ๊ฑธ ๋А๋‚„ ์ˆ˜ ์žˆ์„ ๊ฑฐ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค!๐Ÿ˜„



 

728x90