gpt4 book ai didi

swift - 如何使工作表切换并导航到 View

转载 作者:行者123 更新时间:2023-12-04 08:31:54 26 4
gpt4 key购买 nike

我有 3 个 View A、BSheet、C。
当我单击 A View 中的按钮时,B 工作表变得可见。我在 B 表中有一个按钮。我想在 BSheet View 中按下一个按钮来关闭/切换 BSheet,然后导航到 C View 。任何想法我怎么能做到这一点?
swift

struct A: View{
@State var displayBSheet = false
var body: some View{
NavigationView{
VStack{
Button(action: {self.displayBSheet.toggle()}){
Text("Navigate to BSheet")
}.sheet(isPresented: $displayBSheet){
BSheet()
}
NavgiationLink(destination: C()){
Text("Navigate To C")
}
}
}
}
}
BSheet.swift
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
struct MainView: View {
var body: some View {
Button(action:{
self.presentationMode.wrappedValue.dismiss()//This only sheet but I want to navigate to C View also
}){
Text("Close sheet and navigate to struct C")
}
}
}
swift
struct C: View {
var body: some View {
Text("C")
}
}

最佳答案

我清理了你的代码以获得一个良好的工作:

    import SwiftUI

struct ContentView: View {
var body: some View {

HomeView()

}
}


struct HomeView: View {

@State var homeView: Bool = false

@State var displayViewB: Bool = false
@State var displayViewC: Bool = false



var body: some View{

ZStack {

VStack {

HStack {
Text("Home").font(Font.largeTitle.bold()).padding()
Spacer()
}

Spacer()
}

VStack {
Button(action: { homeView = true; displayViewB = true }) {
Text("open ViewB")
}
.sheet(isPresented: $homeView) {


if displayViewB
{
ViewB(homeView: $homeView, displayViewB: $displayViewB, displayViewC: $displayViewC)
}


if displayViewC
{
ViewC(homeView: $homeView, displayViewC: $displayViewC)
}
}

}


}
.onChange(of: homeView) { _ in
if displayViewB == false && displayViewC == true {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {homeView = true}
}
else if displayViewB == false && displayViewC == false {
homeView = false
}
}
}
}



struct ViewB: View {

@Binding var homeView: Bool
@Binding var displayViewB: Bool
@Binding var displayViewC: Bool

var body: some View {

Button(action:{

displayViewB = false

displayViewC = true

homeView = false


}){
Text("close ViewB and open Viewc")
}


}
}






struct ViewC: View {

@Binding var homeView: Bool
@Binding var displayViewC: Bool

var body: some View {

Button(action:{

displayViewC = false
homeView = false

}){
Text("close ViewC go to HomeView")
}


}
}

关于swift - 如何使工作表切换并导航到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64978341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com