Thursday, August 11, 2022
HomeiOS Developmentios - SwiftUI - Opening sheet with out button click on

ios – SwiftUI – Opening sheet with out button click on


I’m able to sheet over present view after I click on on a button.

Nonetheless I’ve situations the place I question for knowledge and as quickly as knowledge is acquired, as an alternative of exhibiting a message within the view, I want to open a sheet programmatically with none consumer interplay and present a brief message on the sheet to let the consumer know that their motion was profitable. My code for opening the sheet programmatically is not working.

Code for opening sheet through button click on(this code works):

import SwiftUI

struct CustomView: View {
    @State var showingModal: Bool = false
    
    var physique: some View {
        VStack {
            CustomButton(imageName: imageName, motion: { self.showingModal.toggle() })
            .sheet ( isPresented: $showingModal,
                content material: {
                    let messageViewDetails = getMessageViewDetails()

                    ShowMessageAlert(rawValue: messageViewDetails.actionType.rawValue)?.showGeneralMessageView(messageText: messageViewDetails.messageText, showingModal: $showingModal)
                }
            )
        }
    }

    // func for getMessageViewDetails
}

Beneath 2 structs is the code for opening sheet programmatically:

import SwiftUI

struct OtherCustomView: View {
    @ObservedObject var viewModel: CustomViewModel

    var physique: some View {
        VStack {
            VStack {
                HStack {
                    Spacer()
                    Textual content("Hiya World").font(.callout).foregroundColor(Coloration.white)
                    Spacer()
                }
            }.padding(.backside, 10).padding(.prime, 10)

            // Eliminated different states to maintain the code brief and simple to know
            swap viewModel.state {
            case .idle:
                Coloration.clear.onAppear(carry out: { viewModel.getData() })
            case .loading:
                ProgressView()
            case .response(let isSuccess, let consequence):
                ShowResponseView(isSuccess: isSuccess, consequence: consequence)
            }
        }.navigationBarTitle("Again")
        .onDisappear() {
            viewModel.state = .idle
        }
    }
}

// Code for ShowResponseView which shall be reused for different states as nicely in OtherCustomView

import SwiftUI

struct ShowResponseView: View {
    @State var showingModal: Bool = false
    var isSuccess: Bool = false
    var consequence: CustomModel

    var physique: some View {
        VStack {
            let _ = NSLog("Inside VStack of ShowResponseView")
            Textual content("")
        }.onAppear(carry out: {
            self.showingModal.toggle()
        }).sheet(isPresented: $showingModal, content material: {
            let messageViewDetails = getMessageViewDetails()

            let _ = NSLog("Inside sheet of ShowResponseView")
            ShowMessageAlert(rawValue: messageViewDetails.actionType.rawValue)?.generalMessageView(messageText: messageViewDetails.messageText, showingModal: $showingModal)
        })
    }

    // func for getMessageViewDetails
}

Each time response is acquired code inside .response of OtherCustomView will get executed which calls ShowResponseView. I see “Inside VStack of ShowResponseView” getting printed on console, however I by no means see “Inside sheet of ShowResponseView”. I additionally added debug assertion inside .onAppear() and I see that being printed too. I simply do not see the sheet being introduced.

Am I doing something unsuitable right here? How do I current a sheet programmatically?

Observe: Engaged on watch app and on watchOS 7.0 model. Xcode – 13.4.1

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments