狀態變數:
@State var toggleIsOn = false
切換:
Toggle(isOn: $toggleIsOn, label: {Text("Notifications")})
我想要以下按鈕,代表開關的每個狀態,打開和關閉:
// Will request to send notifications, if success; will schedule them.
Button("request") {
NotificationManager.instance.requestAuthorization()
}
// Will clear the queue of notifications, and delete any delivered.
Button("cancel") {
NotificationManager.instance.cancelNotifications()
}
我在這個視頻中找到了函式:https : //www.youtube.com/watch?v=mG9BVAs8AIo
uj5u.com熱心網友回復:
用
onChange(of:)
例如:
struct ContentView: View {
@State var toggleIsOn: Bool = false
var body: some View {
Toggle(isOn: $toggleIsOn, label: {Text("Notifications")})
.onChange(of: toggleIsOn) { isOn in
if isOn {
NotificationManager.instance.requestAuthorization()
} else {
NotificationManager.instance.cancelNotifications()
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/370234.html
上一篇:在熊貓累積周級別應用函式
