Friday, November 6, 2026
HomeiOS Developmentios - SwiftUI: @State variable doesn't replace View when its worth adjustments...

ios – SwiftUI: @State variable doesn’t replace View when its worth adjustments (inside identical View)

[ad_1]

I’m very new to SwiftUI. Possibly I’m not fully understanding how @State annotated variables work. So far as I do know, if you replace their values, in the event that they’re throughout the identical View, the display will get up to date too (on this case the background coloration of the display). I believe I adopted precisely the video tutorial (SwiftUI Fundamentals Tutorial) however nonetheless I didn’t get my code to work. By the best way, I am utilizing Xcode Model 13.2.1, if it is value mentioning. In my ContentView.swift that is what I’ve:

//
//  ContentView.swift
//


import SwiftUI

struct ContentView: View {
     
    @State non-public var isNight = false //Variable to be modified when urgent the button
    
    var physique: some View {
        ZStack {
            //The BackgroundView coloration ought to change when urgent the button
            BackgroundView(topColor: isNight ? .black : .blue,
                           bottomColor: isNight ? .grey : Colour("lightBlue"))
            VStack{
                CityTextView(cityName: "Cupertino, CA")
                MainWeatherStatusView(imageName: "cloud.solar.rain.fill",
                                      temperature: 72)
                HStack(spacing: 0){
                    WeatherView(dayOfWeek: "TUE",
                                imageName: "cloud.solar.fill",
                                temperature: 74)
                    WeatherView(dayOfWeek: "WED",
                                imageName: "solar.max.fill",
                                temperature: 30)
                    WeatherView(dayOfWeek: "THU",
                                imageName: "wind.snow",
                                temperature: 5)
                    WeatherView(dayOfWeek: "FRI",
                                imageName: "sundown.fill",
                                temperature: 60)
                    WeatherView(dayOfWeek: "SAT",
                                imageName: "snow",
                                temperature: 74)
                }
                Button{
                    isNight.toggle() //Altering this could replace my View, proper?
                } label: {
                    WeatherButton(title: "Change Day Time",
                                  textColor: .blue,
                                  backgroundColor: .white)
                }
                Spacer()
            }
        }
        
    }
    
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct WeatherView: View {
    
    var dayOfWeek: String
    var imageName: String
    var temperature: Int
    
    var physique: some View {
        VStack{
            Textual content(dayOfWeek)
                .font(.system(measurement: 15, weight: .medium, design: .default))
                .foregroundColor(.white)
                .padding()
            
            Picture(systemName: imageName)
                .renderingMode(.authentic)
                .resizable()
                .aspectRatio(contentMode: .match)
                .body(width: 40, peak: 40)
            
            Textual content("(temperature)°")
                .font(.system(measurement: 30, weight: .medium))
                .foregroundColor(.white)
            
            Spacer()
        }
    }
}

struct BackgroundView: View {
    
    var topColor: Colour
    var bottomColor: Colour
    
    var physique: some View {
        LinearGradient(gradient: Gradient(colours: [.blue, Color("lightBlue")]), startPoint: .topLeading, endPoint: .bottomTrailing)
            .edgesIgnoringSafeArea(.all)
    }
}

struct CityTextView: View {
    
    var cityName: String
    
    var physique: some View {
        Textual content(cityName)
            .font(.system(measurement: 32, weight: .medium, design: .default))
            .foregroundColor(.white)
            .padding()
    }
}

struct MainWeatherStatusView: View {
    
    var imageName: String
    var temperature: Int
    
    
    var physique: some View {
        VStack(spacing: 8){
            Picture(systemName: imageName)
                .renderingMode(.authentic)
                .resizable()
                .aspectRatio(contentMode: .match)
                .body(width: 180, peak: 180)
            Textual content("(temperature)°")
                .font(.system(measurement: 70, weight: .medium))
                .foregroundColor(.white)
        }
    }
}

In case you need to copy paste the code and need to eliminate the icons, here is the code with out them:

    //
    //  ContentView.swift
    //
    
    
    import SwiftUI
    
    struct ContentView: View {
         
        @State non-public var isNight = false //VARIABLE TO BE CHANGED WHEN PRESSING BUTTON
        
        var physique: some View {
            ZStack {
                //The BackgroundView coloration ought to change when urgent the button
                BackgroundView(topColor: isNight ? .black : .blue,
                               bottomColor: isNight ? .grey : Colour("lightBlue"))
                VStack{
                    CityTextView(cityName: "Cupertino, CA")
                    Button{
                        isNight.toggle() //CHANGING THIS SHOULD UPDATE MY VIEW, RIGHT?
                    } label: {
                        WeatherButton(title: "Change Day Time",
                                      textColor: .blue,
                                      backgroundColor: .white)
                    }
                    Spacer()
                }
            }
            
        }
        
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
    
    struct WeatherView: View {
        
        var dayOfWeek: String
        var imageName: String
        var temperature: Int
        
        var physique: some View {
            VStack{
                Textual content(dayOfWeek)
                    .font(.system(measurement: 15, weight: .medium, design: .default))
                    .foregroundColor(.white)
                    .padding()
                
                Picture(systemName: imageName)
                    .renderingMode(.authentic)
                    .resizable()
                    .aspectRatio(contentMode: .match)
                    .body(width: 40, peak: 40)
                
                Textual content("(temperature)°")
                    .font(.system(measurement: 30, weight: .medium))
                    .foregroundColor(.white)
                
                Spacer()
            }
        }
    }
    
    struct BackgroundView: View {
        
        var topColor: Colour
        var bottomColor: Colour
        
        var physique: some View {
            LinearGradient(gradient: Gradient(colours: [.blue, Color("lightBlue")]), startPoint: .topLeading, endPoint: .bottomTrailing)
                .edgesIgnoringSafeArea(.all)
        }
    }
    
    struct CityTextView: View {
        
        var cityName: String
        
        var physique: some View {
            Textual content(cityName)
                .font(.system(measurement: 32, weight: .medium, design: .default))
                .foregroundColor(.white)
                .padding()
        }
    }
    
    struct MainWeatherStatusView: View {
        
        var imageName: String
        var temperature: Int
        
        
        var physique: some View {
            VStack(spacing: 8){
                Picture(systemName: imageName)
                    .renderingMode(.authentic)
                    .resizable()
                    .aspectRatio(contentMode: .match)
                    .body(width: 180, peak: 180)
                Textual content("(temperature)°")
                    .font(.system(measurement: 70, weight: .medium))
                    .foregroundColor(.white)
            }
        }
    }

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments