반응형
NavigationLink 첫 번째 화면에서 두 번째 화면으로
두 번째 화면에서 첫 번째 화면으로 데이터 전송
struct ContentView: View {
@State var isLinkActive = false
@State private var data: String = ""
var body: some View {
NavigationView {
VStack(alignment: .leading) {
TextField("데이터 입력", text: $data)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
NavigationLink(destination: SecondView(data: $data, isLinkActive: $isLinkActive), isActive: $isLinkActive) {
Button(action: {
self.isLinkActive = true
}) {
Text("Login")
.font(.title)
.padding()
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(10)
.shadow(color: .gray, radius: 5, x: 0, y: 2)
.padding(.horizontal)
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.white)
// .edgesIgnoringSafeArea(.all)
}
}
}
struct SecondView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
@Binding var data: String
@Binding var isLinkActive: Bool
@State private var newData: String = ""
var body: some View {
VStack {
Text("두 번째 화면")
.font(.title)
.padding()
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(10)
.shadow(color: .gray, radius: 5, x: 0, y: 2)
.padding(.horizontal)
Text("전달된 데이터: \(data)")
.foregroundColor(.gray)
TextField("데이터 입력", text: $newData)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
Button(action: {
// 데이터 업데이트 후 이전 화면으로 이동
data = newData // 원하는 로직으로 데이터 업데이트
// 이전화면 이동
isLinkActive = false
//presentationMode.wrappedValue.dismiss()
}) {
Text("첫 번째 뷰로 이동")
.font(.headline)
.foregroundColor(.white)
.padding()
.background(Color.green)
.cornerRadius(10)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.white)
// .edgesIgnoringSafeArea(.all)
// .navigationBarBackButtonHidden(true)
}
}
반응형
'개발 > SWIFT 아이폰' 카테고리의 다른 글
SwiftUI 바텀 네비게이션 TabView 만들기 (0) | 2023.07.20 |
---|---|
SwiftUi Status Bar, Safe Area, Bottom Area 색상 변경 (0) | 2023.07.19 |
SwiftUI NavigationLink를 사용하여 뷰 전환 (0) | 2023.07.19 |
SwiftUI 네이버 지도 호출 및 경로 안내 (0) | 2023.07.18 |
SwiftUI 카카오 내비 호출 및 경로 안내 (0) | 2023.07.18 |