Voici un petit code pour une petite expérience :
struct ContentView: View {
@State var asustor: Bool = false
@State var lightMode: Bool = true
@State var isReceive: Bool = true
var body: some View {
NavigationStack{
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
VStack{
TabView {
ReceivedView(lightMode: $lightMode)
.badge(2)
.tabItem {
Label("Received", systemImage: "tray.and.arrow.down.fill")
}
SentView()
.tabItem {
Label("Sent", systemImage: "tray.and.arrow.up.fill")
}
AccountView()
.badge("!")
.tabItem {
Label("Account", systemImage: "person.crop.circle.fill")
}
}
}
.padding()
}
}
}
Voici le code de ReceivedView, qui comporte un bouton :
struct ReceivedView: View {
@State var isReceive: Bool = false
@Binding var lightMode: Bool
var body: some View {
var affiche: String = "Hello, World"
Image(systemName: "tray.and.arrow.down.fill")
Text(affiche)
myButton(action: {if isReceive == false {affiche = "No boost"}else{affiche=" Super Booooost!!!"}}, title: "Boost", name: "hare", lightMode: $lightMode, invalidate: $isReceive)
}
}
Puis le code de myButton:
struct myButton: View{
var action: () -> Void
var title: String
var name: String
@Binding var lightMode: Bool
@Binding var invalidate: Bool
var body: some View{
if invalidate == true{
Button(action: {()}){
Image(systemName: name)
.fixedSize()
Text(title)
.font(.title)
.fontWeight(/*@START_MENU_TOKEN@*/.bold/*@END_MENU_TOKEN@*/)
// Button end
}.frame(width: 150, height: 40)
.scaleEffect(0.8)
.overlay(Capsule().stroke((lightMode ? Color.black : Color.white)))
.opacity(0.5)
.foregroundColor(lightMode ? .black : .white)
}else{
Button(action: action){
Image(systemName: name)
.fixedSize()
Text(title)
.font(.title)
.fontWeight(/*@START_MENU_TOKEN@*/.bold/*@END_MENU_TOKEN@*/)
// Button end
}.frame(minWidth: 150, idealWidth: 160, maxWidth: 300, idealHeight: 40)
.scaleEffect(0.8)
.overlay(Capsule().stroke((lightMode ? Color.black : Color.white)))
.animation(.default, value: true)
.foregroundColor(lightMode ? .black : .white)
}
}// body end
}// myButton end
Pour résumer, la TabView renvoie vers 3 vues dont une qui comporte un bouton, lequel comporte un Binding sur un wrapper portant un autre nom (Cf. la conférence WWDC 2022 avec le passage sur l’identité des objets dans SwiftUI) et voilà la copie d’écran qui en résulte !..