LoginSignup
0
1

More than 1 year has passed since last update.

【SwiftUI】@Bindingの使い方

Last updated at Posted at 2022-09-01

はじめに

@Binding の使い方

異なるView間で値を共有する方法として@Bindingを用いる

環境

SwiftUI
Xcode: 13.3.1

Code

@Stateを用いて値を渡す場合

HomeView
struct HomeView: View {

    @State var showNextPage = false
    @State var button = false
    
    var body: some View {
        NavigationView{
            VStack{
                Text("Home page")
                Button {
                    self.button.toggle()
                } label: {
                    Text(self.button.description)
                        .foregroundColor(Color.white)
                        .padding()
                }
                .padding()
                .background(self.button ? Color.blue : Color.red)
                .cornerRadius(30)
                
                NavigationLink("go next page", isActive: $showNextPage) {
                    NextPageView(button: $button)
                }
            }
        }
    }
}

NextPageView
struct NextPageView: View {
    
    @State var button : Bool
    
    init(button: Bool ){
        self.button = button
    }
    
    var body: some View {
        VStack{
            Text("Next Page")
            Button {
                self.button.toggle()
            } label: {
                Text(self.button.description)
                    .foregroundColor(Color.white)
                    .padding()
            }
            .padding()
            .background(self.button ? Color.blue : Color.red)
            .cornerRadius(30)
        }
    }
}

Sep-01-2022 19-25-27.gif

HomeView -> NextPageView へは値が影響するが、
HomeView <- NextPageView へは値が影響しない。

   
   

@Bindingの場合

HomeView
struct HomeView: View {

    @State var showNextPage = false
    @State var button = false
    
    var body: some View {
        NavigationView{
            VStack{
                Text("Home page")
                Button {
                    self.button.toggle()
                } label: {
                    Text(self.button.description)
                        .foregroundColor(Color.white)
                        .padding()
                }
                .padding()
                .background(self.button ? Color.blue : Color.red)
                .cornerRadius(30)
                
                NavigationLink("go next page", isActive: $showNextPage) {
                    NextPageView(button: $button)
                }
            }
        }
    }
}

NextPageView
struct NextPageView: View {
    
    @Binding var button : Bool
    
    init(button: Binding<Bool> = .constant(false) ){
        _button = button
    }
    
    var body: some View {
        VStack{
            Text("Next Page")
            Button {
                self.button.toggle()
            } label: {
                Text(self.button.description)
                    .foregroundColor(Color.white)
                    .padding()
            }
            .padding()
            .background(self.button ? Color.blue : Color.red)
            .cornerRadius(30)
        }
    }
}

Sep-01-2022 19-12-53.gif

View間の値が互いに影響する。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1