[SwiftUI] Button과 Navigation을 함께 쓰는법

힐링힐링·2023년 12월 10일
0

SwiftUI

목록 보기
9/14

StartView -> TestView


struct StartView: View {
    @StateObject var vm: APIViewModel = APIViewModel()
    @State private var isSearchButtonTapped = false

    var body: some View {
        NavigationStack{
            VStack{
                Button {
                    isSearchButtonTapped = true
                    vm.search(query: "", srchPolyBizSecd: "")
                } label: {
                    Text("정책검색")
                        .font(.title3)
                        .padding()
                        .background(.buttonGreen)
                        .foregroundColor(.white)
                        .clipShape(RoundedRectangle(cornerRadius: 10))
                }
                .frame(width: 140, height: 50)
                .navigationDestination(isPresented: $isSearchButtonTapped) {
                    // 이동할 뷰 (현재 임시 뷰 지정)
                    TestView().environmentObject(vm)
                }
            }
        }
    }
}

struct TestView: View {
    @EnvironmentObject var vm: APIViewModel
    
    var body: some View {
        ScrollView {
            ForEach(vm.result?.youthPolicies ?? []) { youthPolicy in
                HStack {
                    Text(youthPolicy.polyBizSjnm)
                        .font(.system(size: 15))
                        .padding()
                    
                    Spacer()
                }
            }
        }

    }
}
profile
블로그 이전합니다 https://james-kim-tech.tistory.com/

0개의 댓글