English 中文(简体)
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 42 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
原标题:Toggle Not changing Binding Bool Value
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

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 39 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE


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
问题回答

MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 56 MINUTES 37 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE

struct AppView: View {
    @StateObject var data = Data()

    var body: some View {
        VStack {
            HomeView()
            SettingsView()
        }
        .environmentObject(data)
    }
}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签