Reputation: 31
import SwiftUI
import AVFoundation
struct HomeView: View {
@State private var text2 = ""
let synthsizer = AVSpeechSynthesizer()
let impactRigid = UIImpactFeedbackGenerator(style: .rigid)
let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
@State private var voices = true
@State private var current_voice = true
@State private var isalertshown = false
@FocusState private var iskeyboard: Bool
@EnvironmentObject var dataa: Data
var body: some View {
NavigationStack{
VStack {
Spacer()
ZStack(alignment: .trailing){
TextField("Enter Text and click Speak", text: $text2)
.textFieldStyle(.roundedBorder)
.keyboardType(.default)
.autocorrectionDisabled()
.multilineTextAlignment(.leading)
.submitLabel(.done)
.focused($iskeyboard)
if !text2.isEmpty {
Button {
text2 = ""
}label: {
Image(systemName: "multiply.circle.fill")
.foregroundStyle(Color.gray)
}
.padding(.trailing, 4)
}
}.padding(3)
Button(action: {
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.speak(utterance)
impactRigid.impactOccurred()
}
}) {
Text("Speak")
.font(.title3)
.foregroundColor(.white)
.frame(width: 230, height: 40)
.background(Color.blue)
.cornerRadius(10)
}
//.alert(isPresented: $isalertshown) {
// Alert(title: Text("Error"), message: Text("Please input text in the above field"), dismissButton: .default(Text("OK")))
// }
Spacer()
Text("Current Voice: " + String(current_voice ? "Rishi": "Lekha"))
Menu {
Button {
voices = true
current_voice = true
}
label: {
Text("Rishi")
}
Button {
voices = false
current_voice = false
}
label: {
Text("Lekha")
}
}
label: {
Text("Change Voice")
}.disabled(dataa.changevoice == false)
}
.padding()
.navigationTitle("Text to Speech")
}
}
}
#Preview {
HomeView()
.environmentObject(Data())
}
class Data: ObservableObject {
@Published var changevoice = true
}
import SwiftUI
struct SettingsView: View {
@EnvironmentObject var data: Data
var body: some View {
NavigationView {
List {
Toggle(isOn: $data.changevoice) {
Text("Disable Voice Changing")
}.tint(.blue)
}.navigationTitle("Settings")
}
}
}
#Preview {
SettingsView()
.environmentObject(Data())
}
Text to speech app having having a change voice button to switch between male and female voice, want to disable that button through another view WHAT I AM DOING WRONG?(Beginner) there are two views homeview and settings view, the toggle is on settings view and the button in on homeview, it is only taking the value which i am passing in class
Want to toggle bool value so it can disable button in another view, using published var but no success
struct ContentView: View {
@StateObject var dataa = Data()
var body: some View {
TabView {
HomeView()
.tabItem {Label("Home", systemImage: "house")}
.environmentObject(Data())
SettingsView()
.tabItem {Label("Settings", systemImage: "gear")}
.environmentObject(Data())
}
}
}
#Preview {
ContentView()
.environmentObject(Data())
}```
This is the content view
Upvotes: 0
Views: 89
Reputation: 7170
Without seeing the parent view that owns both of these views, my guess is that you are using different instances of your Data
class, similar to what you are doing in your previews with .environmentObject(Data())
. You need to ensure that these two views are referencing the same instance. This can be done by using a @StateObject
in your parent view, e.g.
struct AppView: View {
@StateObject var data = Data()
var body: some View {
VStack {
HomeView()
SettingsView()
}
.environmentObject(data)
}
}
Upvotes: 0