如何在SwiftUI中自动显示模态视图,无需按钮

3
我考虑使用switch case来呈现不同的视图。
struct searchview : View {

 var body: some View {
      VStack {

            VStack {
                     if self.speechRecognition.isPlaying == true {
                         VStack {
                             Text(self.speechRecognition.recognizedText).bold()
                            .foregroundColor(.white)
                            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
                            .font(.system(size: 50))
                            
                            .sheet(isPresented: $showSheet) {
                                self.sheetView
                            }
                         }.onAppear {
                             DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                                 self.showSheet = true
                             }
                         }
                        }
       }.onAppear(perform: getViews)

 
 }
     
var currentSheetView: String {
    var isProductDictEmpty = Global.productDict.isEmpty
    var wasTextRecognizedEmpty = self.speechRecognition.recognizedText.isEmpty
    var checkTextandDict = (wasTextRecognizedEmpty,isProductDictEmpty)
 

    switch checkTextandDict {

    case (true,true):

        print("Product dict and Text rec are empty")
        return "error"

    case (false,false):
        print("Yes we are in business")
        return "product"


    case (true,false):
        print("OOPS we didnt catch that")
        return "error"
        
    case (false,true):
        print("OOPS we didnt catch that")
    return "zero match"
    
        
    }
  }
   @ViewBuilder
   var sheetView: some View {

    if currentSheetView == "product"  {
          ProductSearchView(model: self.searchModel)
      }
    else if currentSheetView == "zero match" {
         zeroResult()
    }
    else if currentSheetView == "error" {
        SearchErrorView()
    }
    
    
  }
    
     }

 }

我知道如何使用.sheet 修改器在按下按钮时呈现模态视图,但是如何能自动通过按钮呈现相应的模态视图?

更新

这些是我正在尝试实现的视图。

struct SearchErrorView: View {
   var body: some View {
      VStack {
          Text("Error!")
          Text("Oops we didn't catch that")
      }
    }
 }

struct zeroResult: View {
  var body: some View {
    Text("Sorry we did not find any result for this item")
   }
}

我不确定我错在了哪里。我试图实现以下解决方案,但仍然无法使用 switch case 调用视图。

1个回答

4
解决方案是通过编程设置控制显示表格的变量。您可以尝试在onAppear中采用以下方式展示您的表格:
struct ContentView: View {
    @State var showSheet = false

    var body: some View {
        Text("Main view")
            .sheet(isPresented: $showSheet) {
                self.sheetView
            }
            .onAppear {
                self.showSheet = true
            }
    }
    
    var currentSheetView: String {
        "view1" // or any other...
    }

    @ViewBuilder
    var sheetView: some View {
        if currentSheetView == "view1" {
            Text("View 1")
        } else {
            Text("View 2")
        }
    }
}

你也可以将其延迟:

.onAppear {
    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
        self.showSheet = true
    }
}

只需设置 self.showSheet = true,就可以显示表单。如何触发它取决于您。


在 switch 语句检查之前,我并不总是知道要展示哪个视图。因此,Text("Sheet view") 将在 getViews 函数中确定。如何在函数中实现 .sheet 呢? - undefined
我尝试着实现了你的解决方案,但是我不知道我漏掉了什么。我已经更新了我的详细代码,让你看看我误解了什么。 - undefined
@learner101 不好意思,目前 switch 无法与 @ViewBuilder 一起使用。我已将其替换为 if。请查看更新后的答案。 - undefined
我不知道你是否有机会看到更新的问题,它展示了我代码的更多细节。 - undefined
我更新了问题,并且做出了修改。但是视图仍然没有显示。 - undefined
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接