Sunday, September 25, 2022
HomeiOS Developmentios - SwiftUI - onChange not getting known as

ios – SwiftUI – onChange not getting known as


Including the code first to assist higher perceive the issue.

struct MainSetupView: View {
    @StateObject var viewModel: MainViewModel
    
    var physique: some View {
        VStack {
            change viewModel.state {
            case .idle:
                Colour.clear.onAppear(carry out: { viewModel.put together() })
            case .loading:
                ProgressView()
            case .showMessage(let messageType, let messageText):
                ErrorHandlingView(messageType: messageType, messageText: messageText)
            }
        }.onAppear() {
            viewModel.state = .idle
        }
    }
}

struct ErrorHandlingView: View {
    @State var messageType: String
    var messageText: String

    @State non-public var messageViewImageDetails: (imageName: String, isSystemImage: Bool) = (imageName: "", isSystemImage: false)

    var physique: some View {
        VStack {
            MessageViewWithActions(imageName: messageViewImageDetails.imageName, isSystemImage: messageViewImageDetails.isSystemImage, mainMessageText: messageText)
        }.onChange(of: messageType) {_ in
            self.messageViewImageDetails = getImageForError()
            // name extra features
        }
    }
    
    non-public func getImageForError() -> (imageName: String, isSystemImage: Bool) {
        var imageName = ""
        var isSystemImage = false
        
        change messageType {
        case "noInternetError":
            imageName = "xmark.icloud"
            isSystemImage = true
        case "userSignedOut":
            imageName = "individual.crop.circle.fill.badge.xmark"
            isSystemImage = true
        case "userSignedIn":
            imageName = "individual.crop.circle.badge.checkmark"
            isSystemImage = true
        // extra circumstances.
        default:
            imageName = "data.circle"
            isSystemImage = true
        }
        
        return (imageName: imageName, isSystemImage: isSystemImage)
    }
}

If “MainSetupView” is open and I obtain a message i.e. .showMessage, “ErrorHandlingView” is displayed and I set “messageType” and “messageText” whereas calling ErrorHandlingView. Whereas first message is being displayed, for example I obtain one other message and “ErrorHandlingView” will get known as once more, now with a special message sort. Nonetheless “onChange(of: messageType)” in “ErrorHandlingView” by no means will get known as.

“onChange(of: messageType)” calls a operate “getImageForError” which tells the app which picture to show based mostly on the worth of messageType. Since code in onChange isn’t getting executed, “getImageForError” by no means will get known as and therefore “messageViewImageDetails” isn’t populated which leads to view with out picture.

Any concept why is .onChange() not being executed? What am I doing improper?

Notice: I’ve numerous code and I’m sharing items of code which I assumed could be useful. Let me know if extra code is required.

Edit: Including code for MessageViewWithActions.

struct MessageViewWithActions: View {
    // Picture properties
    var imageName: String?
    var isSystemImage: Bool = false
    
    // Textual content properties
    var mainMessageText: String?

    var physique: some View {
        ScrollView {
            VStack {
                Textual content("")
                VStack {
                    if let imageName = imageName {
                        if isSystemImage {
                            Picture(systemName: imageName)
                        } else {
                            Picture(imageName)
                        }
                    }
                }.body(maxWidth: .infinity).padding(.backside, 8)
                
                if let mainMessageText = mainMessageText {
                    Textual content(mainMessageText).font(.subheadline).fixedSize(horizontal: false, vertical: true)
                }
            }
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular