简体   繁体   English

以编程方式呈现 SwiftUI 视图不起作用

[英]Programmatically presenting SwiftUI view is not working

I have a SwiftUI view that I want to display programmatically.我有一个想要以编程方式显示的 SwiftUI 视图。 I currently present the screen with a button but I want to present it programmatically我目前用一个按钮呈现屏幕,但我想以编程方式呈现它

This is the view:这是视图:

struct SearchResultView: View {
@ObservedObject var model: SearchResultViewModel

    var body: some View {
        
        VStack {
            VStack{
                Text("Check Aisles ")
                HStack{
                    ForEach(0 ..< model.aisleArry.count){aisleNum in
                        Text(String(self.model.aisleArry[aisleNum])).bold()
                    }
                }
                Text( "For Blue Coffee")
            }
            Spacer()
            VStack {
                ForEach(0 ..< Global.productArry.count) { value in
                    Text(Global.productArry[value].name)
                    
                  }
               }
            
            Spacer()
  
            }.onAppear { self.model.getValue() }
        }
  }

This is the button that presents it:这是显示它的按钮:

struct homeMainView: View {

   var body: some View {

    Button("Search") {
       // show the search sheet
       self.searchSheet.toggle()
     }
    .sheet(isPresented: $searchSheet) {        
       ProductSearchView(model: self.searchModel)
             //
      }


       VStack {
            
            if self.speechRecognition.isPlaying {
             
                VStack {
                    Text(self.speechRecognition.recognizedText).bold()
                       
                            
                }.onAppear{
                    //if text is recognize wait a few seconds and launch searchResult View
                    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                        // wait 1 second
                  // I WANT TO PRESENT IT PROGRAMATICALLY HERE
    ProductSearchView(model:self.searchModel).presentationMode.wrappedValue.isPresented
           self.sheet(isPresented: self.$searchSheet) {
      ProductSearchView(model: self.searchModel)

                    }
                        
                    }
                }
            }

   }
}

I have tried many methods but could not get it to work.我尝试了很多方法,但无法让它发挥作用。 How can I present the SearchResultView programmatically?如何以编程方式呈现SearchResultView

Here is a possible solution这是一个可能的解决方案

    VStack {
        if self.speechRecognition.isPlaying {
            VStack {
                Text(self.speechRecognition.recognizedText).bold()
            }.onAppear{
                //if text is recognize wait a few seconds and launch searchResult View
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    // wait 1 second
                    self.searchSheet.toggle()        // << activate here !!

                }
            }
        }
    }
    .sheet(isPresented: $searchSheet) {     // << attach here !!
        ProductSearchView(model: self.searchModel)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM