ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Swift] State์™€ Binding์ด ๋ฌด์—‡์ผ๊นŒ?
    ๐Ÿ’ปProgramming/Swift 2024. 3. 18. 17:19
    ๋ฐ˜์‘ํ˜•

    Property Mapper(@)

    ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ €์žฅ๋˜๋Š” ๋ฐฉ์‹์„ ๊ด€๋ฆฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€์‹œ์ผœ ์ฃผ๋Š” ๊ฒƒ.(์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ)

    @State

    SwiftUI์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋Š” property wrapper ํƒ€์ž…

    ๋ณ€์ˆ˜๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ, View์—์„œ๋„ update ๋˜๋ฉด์„œ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๊ธธ ์›ํ•  ๋•Œ ์‚ฌ์šฉ.

    @State ์‚ฌ์šฉ

    struct StateBasic: View {
        // property
        @State var backgroundColor: Color = Color.green
        @State var myTitle: String = "์•„์ง ๋ฒ„ํŠผ ์•ˆ๋ˆŒ๋ฆผ"
        @State var count: Int = 0
    }

     

    ์™€ ๊ฐ™์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅ.

    HStack (spacing: 20) {
        Button {
            // action
            backgroundColor = .red
            myTitle = "1๋ฒˆ ๋ฒ„ํŠผ ๋ˆŒ๋ฆผ"
            count += 1  // count = count + 1
        } label: {
            Text("1๋ฒˆ ๋ฒ„ํŠผ")
        }
    
        Button {
            // action
            backgroundColor = .purple
            myTitle = "2๋ฒˆ ๋ฒ„ํŠผ ๋ˆŒ๋ฆผ"
            count -= 1
        } label: {
            Text("2๋ฒˆ ๋ฒ„ํŠผ")
        }
    }

    ์ด์™€ ๊ฐ™์ด ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ฃผ๊ฒŒ ๋˜๋ฉด, 

    ํ™”๋ฉด์— ๊ด€๋ จ๋œ ์„ค์ •์„ ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋ฐ”๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค.

    @State ์‚ฌ์šฉ ๊ฒฐ๊ณผ

     

    ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ๋ณ€๊ฒฝ๋œ ๊ฐ’์ด ๋ฐ”๋กœ ํ™”๋ฉด์— ๋ฐ˜์˜๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

    @Binding

    @State๋ฅผ @SubView(ํ•˜์œ„ ๋ทฐ)์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ Wrapper

    @Binding ์‹ค์Šต

    ํ•˜์œ„ ๋ทฐ ์ฝ”๋“œ

    import SwiftUI
    
    struct BindingChild: View {
        // property
        @State var buttonColor: Color = Color.blue
        @Binding var backgroundColor: Color
        @Binding var title: String
        
        var body: some View {
            Button {
                // action
                backgroundColor = .orange
                buttonColor = .pink
                title = "Binding Child View"
            } label: {
                Text("Child View ์ด๋™")
                    .foregroundColor(.white)
                    .padding()
                    .padding(.horizontal)
                    .background(buttonColor)
                    .cornerRadius(10)
            }
        }
    }

    ์ƒ์œ„ ์ฝ”๋“œ์—์„œ ๋„˜๊ฒจ์ค„ ๋ณ€์ˆ˜์— @Binding์„ ๋ถ™์ธ๋‹ค. ์ด ๋•Œ, ์ƒ์œ„ ์ฝ”๋“œ์—์„œ ๋„˜๊ฒจ์ฃผ๋Š” ๋ณ€์ˆ˜๋ช…๊ณผ ํ•˜์œ„ ์ฝ”๋“œ์˜  @Binding ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ ๊ฐ™์•„์•ผํ•œ๋‹ค.

    ํ•˜์œ„ ๋ทฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ƒ์œ„ ๋ทฐ ์ฝ”๋“œ

    struct BindingBasic: View {
        // property
        @State var backgroundColor: Color = Color.green
        @State var title: String = "Binding Basic View"
        
        var body: some View {
            ZStack {
                // background
                backgroundColor
                    .ignoresSafeArea()
                
                // content
                VStack {
                    Text(title)
                    
                    // button
                    // parameter๋กœ @State์˜ ๊ฐ’์— $๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค.
                    BindingChild(backgroundColor: $backgroundColor, title: $title)
                }
            }
        }
    }

    ์˜ BindingChild ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ €๋ ‡๊ฒŒ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ @State์˜ ๋ณ€์ˆ˜๋ช…์— $๋ฅผ ๋ถ™์—ฌ์ฃผ์–ด์„œ ํ•˜์œ„ ๋ทฐ์— ๋„˜๊ฒจ์ค€๋‹ค.

    @Binding ์‚ฌ์šฉ ๊ฒฐ๊ณผ

     

    ํ•˜์œ„ ๋ทฐ์— ๋„˜๊ฒจ์ค€ title๊ณผ backgroundColor๊ฐ€ ํ•˜์œ„ ๋ทฐ์˜ ๋ฒ„ํŠผ ์•ก์…˜์„ ํ†ตํ•ด ์ƒ์œ„ ๋ทฐ์˜ ๊ฐ’๋“ค์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

    ๋ฐ˜์‘ํ˜•

    '๐Ÿ’ปProgramming > Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    [Swift] Sheet๊ณผ FullScreenCover, NavigationView  (0) 2024.03.20
Designed by Tistory.