Sunday, March 17, 2024
HomeiOS Developmentios - Toggle Not altering Binding Bool Worth

ios – Toggle Not altering Binding Bool Worth


import SwiftUI
import AVFoundation

struct HomeView: View {
    @State non-public var text2 = ""
    let synthsizer = AVSpeechSynthesizer()
    let impactRigid = UIImpactFeedbackGenerator(type: .inflexible)
    let impactHeavy = UIImpactFeedbackGenerator(type: .heavy)
    @State non-public var voices = true
    @State non-public var current_voice = true
    @State non-public var  isalertshown = false
    @FocusState non-public var iskeyboard: Bool
    @EnvironmentObject var dataa: Information
    var physique: some View {
        NavigationStack{
            VStack {
                Spacer()
                ZStack(alignment: .trailing){
                    TextField("Enter Textual content and click on Communicate", textual content: $text2)
                        .textFieldStyle(.roundedBorder)
                        .keyboardType(.default)
                        .autocorrectionDisabled()
                        .multilineTextAlignment(.main)
                        .submitLabel(.accomplished)
                        .targeted($iskeyboard)
                    if !text2.isEmpty {
                        Button {
                            text2 = ""
                        }label: {
                            Picture(systemName: "multiply.circle.fill")
                                .foregroundStyle(Shade.grey)
                        }
                        .padding(.trailing, 4)
                    }
                }.padding(3)
                    
                
                Button(motion: {
                    
                    if text2.isEmpty {
                        iskeyboard = true
                        //isalertshown = true
                        impactHeavy.impactOccurred()
                        
                        
                    } else {
                        
                        let utterance = AVSpeechUtterance(string: text2)
                        
                        utterance.voice = AVSpeechSynthesisVoice(identifier: voices ? "com.apple.ttsbundle.Rishi-compact": "com.apple.ttsbundle.Lekha-compact")
                        
                        synthsizer.communicate(utterance)
                        impactRigid.impactOccurred()
                        
                    }
                    
                }) {
                    
                    Textual content("Communicate")
                        .font(.title3)
                        .foregroundColor(.white)
                        .body(width: 230, top: 40)
                        .background(Shade.blue)
                        .cornerRadius(10)
                        
                }
                
                //.alert(isPresented: $isalertshown) {
                    
                  //  Alert(title: Textual content("Error"), message: Textual content("Please enter textual content within the above subject"), dismissButton: .default(Textual content("OK")))
                    
              //  }
                Spacer()
                Textual content("Present Voice: " + String(current_voice ? "Rishi": "Lekha"))
               
                Menu {
                    Button {
                        voices = true
                        current_voice = true
                    }
                label: {
                    Textual content("Rishi")
                }
                    Button {
                        voices = false
                        current_voice = false
                    }
                label: {
                    Textual content("Lekha")
                }
                }
            label: {
                Textual content("Change Voice")
            }.disabled(dataa.changevoice == false)
            }
            .padding()
            .navigationTitle("Textual content to Speech")
        }
    }
}


#Preview {
    HomeView()
        .environmentObject(Information())
}
class Information: ObservableObject {
    @Printed var changevoice = true
}

import SwiftUI
struct SettingsView: View {
    @EnvironmentObject var information: Information
    var physique: some View {
        NavigationView {
            Record {
                Toggle(isOn: $information.changevoice) {
                    Textual content("Disable Voice Altering")
                }.tint(.blue)
            }.navigationTitle("Settings")
        }
    }
}

#Preview {
    SettingsView()
        .environmentObject(Information())
}

Textual content to speech app having having a change voice button to modify between female and male voice, wish to disable that button by way of one other view
WHAT I AM DOING WRONG?(Newbie)
there are two views homeview and settings view, the toggle is on settings view and the button in on homeview, it’s only taking the worth which i’m passing in school

Wish to toggle bool worth so it might probably disable button in one other view, utilizing revealed var however no success



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments