저는 SwiftUI를 처음 사용합니다. Timer 앱을 진행하려고하는데 막혔습니다.
내 ContentView에는 두 개의 ObservedObject, 타이머 기능을 관리하는 Timer 개체와 모든 사용자 설정을 저장하는 Settings 개체가 있습니다. 내 목표는 사용자가 토글 설정을 변경하면 ContentView 클래스에 정의 된 MyTimer의 인스턴스에 변수가 설정되는 것입니다.
내 문제는 클래스 설정에서 해당 인스턴스에 액세스하는 방법을 모른다는 것입니다.
아마도 매우 쉬운 해결책 일 것입니다. 그러나 저는이 작업을 꽤 오랫동안 시도하고 있으며 운이 좋지 않았습니다.
도움을 주셔서 감사합니다!
struct ContentView: View {
@ObservedObject var myTimer = MyTimer()
@ObservedObject var settings = Settings()
...
Toggle(isOn: self.$settings.muteInSilenceMode) {
Text("Mute sound in silence mode")
}
...
}
class Settings: ObservableObject {
...
@Published var muteInSilenceMode: Bool = UserDefaults.standard.bool(forKey: "muteInSilenceMode"){
didSet{
UserDefaults.standard.set(self.muteInSilenceMode, forKey: "muteInSilenceMode"
// I want to access the Timer object from here
}
}
...
}
귀하의 Settings ()가 귀하의 질문과 어떤 관련이 있는지는 알지 못하지만 귀하의 질문에 대한 답변은 다음과 같습니다.
struct ContentView : View {
@ObservedObject var myTimer = MyTimer()
@ObservedObject var settings = Settings()
@State private var timer: Bool
var body: some View {
let toggleTimer = Binding<Bool> (
get: { self.timer},
set: { newValue in
self.timer = newValue
if self.timer {
myTimer.theVaribableYouWantToChange = true // Didn't know the name of your variable.
} else {
// Whatever shall happen if the Toggle gets deactivated.
}
})
return HStack {
Toggle(isOn: toggleTimer) {Text("Activate") }
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다