Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SwiftUI中无需按钮自动显示模态_Swift_Swiftui - Fatal编程技术网

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

如何在SwiftUI中无需按钮自动显示模态,swift,swiftui,Swift,Swiftui,我想用一个开关盒来表达不同的观点 struct searchview : View { var body: some View { VStack { VStack { if self.speechRecognition.isPlaying == true { VStack { Text(self.speec

我想用一个开关盒来表达不同的观点

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
修改器在按下按钮时显示模态,但如何使用按钮自动显示swift案例中的各个模态

更新

这些是我正在努力实现的观点

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")
   }
}

我不确定我做错了什么。我试图实现下面的解决方案,但仍然无法使用开关盒调用视图。

解决方案是通过编程方式设置一个变量来控制工作表的显示

您可以尝试以下方法在附件中显示您的工作表:

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语句之前,我并不总是知道显示的是什么视图。因此,文本(“图纸视图”)将在func GetView中确定。如何在函数中实现.sheet我试图实现您的解决方案,但我不知道我缺少了什么。我用我的详细代码更新了,以便您查看我misunderstood@learner101我的坏开关,当前的
无法与@ViewBuilder一起使用。我将其替换为
if
。请查看更新后的答案。我不知道您是否有机会看到更新后的问题,这些问题显示了我代码的更多细节。@learner101是的,但这并不重要,因为您的观点是标准的。您可以轻松地将
文本(…)
替换为任何视图(例如
SearchErrorView()
)。这不影响问题。我还添加了一个计算属性,您可以在其中决定显示哪个视图。