1

"Resolving UI Update Issues with iOS 13 ObservedObject: Exploring Alternatives to StateObject"

"I've explored several alternatives for StateObject, which is only available in iOS 14. Unfortunately, I need a solution compatible with iOS 13. Any guidance or suggestions on addressing this problem would be highly appreciated."

How to achieve same thing in iOS 13 with ObservedObject??

I tried for ObservedObject in iOS 13 but that is not working to update the UI.

Code

import SwiftUI

struct AiicoTermsConditionsView: View {
    
    @Environment(\.presentationMode) var presentationMode
    @ObservedObject var viewModel: AiicoTermsConditionsViewModel
    @ObservedObject var webmodel = WebViewModel(url: "")
    
    let backArrowImg = "arrow.left"
    
    var btnBack : some View { Button(action: {
        self.presentationMode.wrappedValue.dismiss()
    }) {
        HStack {
            Image(backArrowImg)
                .aspectRatio(contentMode: .fit)
                .foregroundColor(.colorBlack)
        }
    }
    }
    
    var rightBarButton : some View { Button(action: {
        
    }) {
        HStack {
            viewModel.rightButtonBar()
        }
    }
    }
    
    
    
    var body: some View {
        GeometryReader { geo in
            VStack{
                
                NavigationLink(destination: InsuranceSelectPlanView(viewModel: InsuranceSelectPlanViewModel(rechargeData: viewModel.rechargeData, title: viewModel.title, billerType: viewModel.billerType, isDoctorDay: viewModel.isDoctorDay, selectedPlanData: viewModel.selectedPlanData, selectedHospitalData: viewModel.selectedHospitalData, insuranceType: viewModel.insuranceType, formType: .selfDetailsForm, detailFormModel: viewModel.detailFormModel, KinFormModel: viewModel.KinFormModel, documentFormModel: viewModel.documentFormModel, isFromPreview: false, selectHospitalMode: .purchasePolicy)),  isActive: $viewModel.navigateTONext){
                    EmptyView()
                }
                
                if viewModel.isDoctorDay {
                    Text("docterdey_about_message".localized.toString())
                        .font(.kMontserratMedium14)
                        .padding([.leading,.trailing],20)
                    
                    Spacer()
                }
                else
                {
                    // webview
                    LoadingView(isShowing: $webmodel.isLoading) { WebView(viewModel:  webmodel) }
                }
                
                // Bottom button
                NWRoundedButton(title: kNext,
                                textColor: Color.colorGolden,
                                buttonBackgroundColor: Color.colorBlack,
                                action:
                                    {
                    viewModel.navigateTONext = true
                    
                    
                })
                .frame(height: 70)
            }
            .onAppear(){
                
            }
            .navigationTitle("about_insurance".localized.toString()).font(.kMontserratRegular17)
            .navigationBarTitleDisplayMode(.inline)
            .navigationBarBackButtonHidden(true)
            .navigationBarItems(leading: btnBack , trailing: rightBarButton)
        }
    }
}
4
  • 1
    Please edit your question to show the relevant code. Without seeing your code it is impossible for us to suggest a solution
    – Paulw11
    Commented Jul 26, 2023 at 12:35
  • @Paulw11 can you check one again i add the code in the question Commented Jul 26, 2023 at 13:15
  • You are creating new observed object instances each time the view is modified (because SwiftUI views are structs and therefore immutable). You should put your observed object in the environment in your app so that there is only one instance - in iOS 14 and later you can use @StateObject
    – Paulw11
    Commented Jul 26, 2023 at 19:09
  • @Paulw11 Can tell me how i can do this with code so observed object instance can not be call multiple times Commented Jul 27, 2023 at 4:53

1 Answer 1

0

In iOS 13, you can't use the @StateObject property wrapper because it's introduced in iOS 14. Instead, you can use @ObservedObject in combination with a workaround to achieve similar functionality. Here's how you can modify your code to work with iOS 13:

import SwiftUI

struct AiicoTermsConditionsView: View {
    @Environment(\.presentationMode) var presentationMode
    @ObservedObject var viewModel: AiicoTermsConditionsViewModel
    @ObservedObject var webmodel: WebViewModel
    
    // Initialize webmodel in init
    init() {
        self.webmodel = WebViewModel(url: "")
    }
    
    let backArrowImg = "arrow.left"
    
    var btnBack: some View {
        Button(action: {
            self.presentationMode.wrappedValue.dismiss()
        }) {
            HStack {
                Image(backArrowImg)
                    .aspectRatio(contentMode: .fit)
                    .foregroundColor(.colorBlack)
            }
        }
    }
    
    var rightBarButton: some View {
        Button(action: {
            // Handle right button action here
        }) {
            HStack {
                viewModel.rightButtonBar()
            }
        }
    }
    
    var body: some View {
        GeometryReader { geo in
            VStack {
                NavigationLink(
                    destination: InsuranceSelectPlanView(viewModel: InsuranceSelectPlanViewModel(rechargeData: viewModel.rechargeData, title: viewModel.title, billerType: viewModel.billerType, isDoctorDay: viewModel.isDoctorDay, selectedPlanData: viewModel.selectedPlanData, selectedHospitalData: viewModel.selectedHospitalData, insuranceType: viewModel.insuranceType, formType: .selfDetailsForm, detailFormModel: viewModel.detailFormModel, KinFormModel: viewModel.KinFormModel, documentFormModel: viewModel.documentFormModel, isFromPreview: false, selectHospitalMode: .purchasePolicy)),
                    isActive: $viewModel.navigateTONext
                ) {
                    EmptyView()
                }
                
                if viewModel.isDoctorDay {
                    Text("docterdey_about_message".localized.toString())
                        .font(.kMontserratMedium14)
                        .padding([.leading, .trailing], 20)
                    
                    Spacer()
                } else {
                    // webview
                    LoadingView(isShowing: $webmodel.isLoading) {
                        WebView(viewModel: webmodel)
                    }
                }
                
                // Bottom button
                NWRoundedButton(
                    title: kNext,
                    textColor: Color.colorGolden,
                    buttonBackgroundColor: Color.colorBlack
                ) {
                    viewModel.navigateTONext = true
                }
                .frame(height: 70)
            }
            .onAppear() {
                // Perform any necessary setup here
            }
            .navigationTitle("about_insurance".localized.toString())
            .font(.kMontserratRegular17)
            .navigationBarTitleDisplayMode(.inline)
            .navigationBarBackButtonHidden(true)
            .navigationBarItems(leading: btnBack, trailing: rightBarButton)
        }
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.