article

Wednesday, May 26, 2021

SwiftUI how to call a function when button pressed

SwiftUI how to call a function when button pressed
 
//
//  ContentView.swift
//  Testapp
//
//  Created by Cairocoders
//

import SwiftUI

struct ContentView: View {

    @State private var count = 0
    
    var body: some View {
        
        VStack {
            Button(action: {stepCount()
            }, label: {
                Text("Increment Count")
                    .fontWeight(.heavy)
                    .foregroundColor(.white)
                    .padding()
                    .background(Color.blue)
                    .cornerRadius(15)
            })
            
            Text("The count is: \(self.count)")
        }
    } // End body
    
    func stepCount() {
        count += 1
    }
    
}

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

Related Post