IOS/SwiftUI

(SwiftUI)03.바인딩 @Binding

프린이0218 2021. 4. 27. 12:55
//
//  ContentView.swift
//  YoutubeSwift
//
//  Created by devdepot on 2021/04/27.
//

import SwiftUI

struct ContentView: View {
    
    @State
    private var isActivated: Bool = false;
    @State
    private var testAct: Bool = true;
    var body: some View {
       

        NavigationView{
            VStack{
                HStack{
                    MyVstackViewL(isActivated: $isActivated)    //false넘김
                    MyVstackViewL(isActivated: $testAct)        //true 넘김
                    MyVstackViewL() //false 넘김
                }.padding(10).background(Color.red)
                
                NavigationLink(
                    destination: MyVstackViewL(),
                    label: {
                        /*@START_MENU_TOKEN@*/Text("Navigate")/*@END_MENU_TOKEN@*/
                    })
            }
            
        }
    }
}


struct MyVstackViewL:View {
    
    @Binding
    var isActivated: Bool
    
    //생성자
    init(isActivated: Binding<Bool> = .constant(false)) {
        _isActivated = isActivated
    }
    var body: some View{
        VStack{
            Text("1")
                .fontWeight(.bold)
                .font(.system(size: 60))
            Text("2")
                .fontWeight(.bold)
                .font(.system(size: 60))
            Text("3")
                .fontWeight(.bold)
                .font(.system(size: 60))
        }.background(self.isActivated ? Color.green : Color.yellow)
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}