• 카테고리

    질문 & 답변
  • 세부 분야

    모바일 앱 개발

  • 해결 여부

    미해결

바인딩 값에 특정 하나의 값을 할당하기

21.06.04 09:06 작성 조회수 125

0

선생님^^

MyButton(str: $str)

MyButton(str: “특정값”)

바인딩 된 값은 $변수이름 말고 그냥 값을 넣을 수는 없나요? 

VStack{

MyButton(str: “첫번째단추”)

MyButton(str: “두번째단추”)

}

이렇게 하고 싶은 경우가 있더라구요~

답변 3

·

답변을 작성해보세요.

1

안녕하세요

단순히 버튼에 타이틀을 넣는걸 말씀하시는거면 아래처럼 가능합니다.

      Button("some title") {

             // some action

            }

0

PopItButton(popColor: Color.red) 이렇게 하니까 에러가 뜨더라구요.

안녕하세요

Binding을 받아야 되는 경우에 질문하신 형태로는 가능하지 않습니다.

@State를 통해서 값을 넣는식으로 생각해야 될 것 같습니다.

0

struct ContentView: View {

    

    @State var colorRed = Color.red

    @State var colorOrange = Color.orange

    @State var colorYellow = Color.yellow

    @State var colorGreen = Color.green

    @State var colorBlue = Color.blue

    @State var colorPurple = Color.purple

    

    var body: some View {

        

        ZStack{

        Image("rainbow")

            .resizable()

            .edgesIgnoringSafeArea(.all)

            

            VStack{

                

                PopItButton(popColor: $colorRed)

                PopItButton(popColor: $colorOrange)

                PopItButton(popColor: $colorYellow)

                PopItButton(popColor: $colorGreen)

                PopItButton(popColor: $colorBlue)

                PopItButton(popColor: $colorPurple)

              

            }

            

        }

    }

}

struct PopItButton: View {

    @Binding var popColor: Color

    

    var body: some View {

        HStack{

            ForEach (1..<5) { index in

                aButton()

                    .colorMultiply(popColor)

            }

        }

    }

}

이런경우 어떻게 해야하는지 궁금해서요