Skip to main content
TopMiniSite

Back to all posts

How to Add an Action to A Swiftui View?

Published on
5 min read
How to Add an Action to A Swiftui View? image

Best SwiftUI Development Guides to Buy in February 2026

1 iOS 26 App Development Essentials - SwiftUI Edition: Developing iOS Apps Using SwiftUI, Swift and Xcode 26

iOS 26 App Development Essentials - SwiftUI Edition: Developing iOS Apps Using SwiftUI, Swift and Xcode 26

BUY & SAVE
iOS 26 App Development Essentials - SwiftUI Edition: Developing iOS Apps Using SwiftUI, Swift and Xcode 26
2 Thinking in SwiftUI

Thinking in SwiftUI

BUY & SAVE
Thinking in SwiftUI
3 iOS 26 Programming for Beginners: A hands-on guide to kickstarting your iOS app development journey with Swift 6, UIKit, and Xcode 26

iOS 26 Programming for Beginners: A hands-on guide to kickstarting your iOS app development journey with Swift 6, UIKit, and Xcode 26

BUY & SAVE
Save 20%
iOS 26 Programming for Beginners: A hands-on guide to kickstarting your iOS app development journey with Swift 6, UIKit, and Xcode 26
4 SwiftUI Cookbook: A guide for building beautiful and interactive SwiftUI apps

SwiftUI Cookbook: A guide for building beautiful and interactive SwiftUI apps

BUY & SAVE
Save 38%
SwiftUI Cookbook: A guide for building beautiful and interactive SwiftUI apps
5 SwiftUI for Masterminds 5th Edition: How to take advantage of Swift and SwiftUI to create insanely great apps for iPhones, iPads, and Macs

SwiftUI for Masterminds 5th Edition: How to take advantage of Swift and SwiftUI to create insanely great apps for iPhones, iPads, and Macs

BUY & SAVE
Save 75%
SwiftUI for Masterminds 5th Edition: How to take advantage of Swift and SwiftUI to create insanely great apps for iPhones, iPads, and Macs
6 Xcode and SwiftUI Handbook: A Complete guide to IOS App Development (The Tech Essential Programming Guide)

Xcode and SwiftUI Handbook: A Complete guide to IOS App Development (The Tech Essential Programming Guide)

BUY & SAVE
Xcode and SwiftUI Handbook: A Complete guide to IOS App Development (The Tech Essential Programming Guide)
7 SwiftUI Cookbook: A guide to solving the most common problems and learning best practices while building SwiftUI apps, 2nd Edition

SwiftUI Cookbook: A guide to solving the most common problems and learning best practices while building SwiftUI apps, 2nd Edition

BUY & SAVE
Save 56%
SwiftUI Cookbook: A guide to solving the most common problems and learning best practices while building SwiftUI apps, 2nd Edition
8 SwiftUI by Tutorials (Fifth Edition): Declarative App Development on the Apple Ecosystem

SwiftUI by Tutorials (Fifth Edition): Declarative App Development on the Apple Ecosystem

BUY & SAVE
Save 45%
SwiftUI by Tutorials (Fifth Edition): Declarative App Development on the Apple Ecosystem
9 SwiftUI Cookbook: Discover solutions and best practices to tackle the most common problems while building SwiftUI apps

SwiftUI Cookbook: Discover solutions and best practices to tackle the most common problems while building SwiftUI apps

BUY & SAVE
Save 75%
SwiftUI Cookbook: Discover solutions and best practices to tackle the most common problems while building SwiftUI apps
10 SwiftUI Projects: Build six real-world, cross-platform mobile applications using Swift, Xcode 12, and SwiftUI

SwiftUI Projects: Build six real-world, cross-platform mobile applications using Swift, Xcode 12, and SwiftUI

BUY & SAVE
Save 47%
SwiftUI Projects: Build six real-world, cross-platform mobile applications using Swift, Xcode 12, and SwiftUI
+
ONE MORE?

To add an action to a SwiftUI view, you can use the onTapGesture modifier to specify a closure that will be executed when the view is tapped by the user. Inside this closure, you can place any code that you want to be executed when the action occurs. For example:

struct ContentView: View { var body: some View { Text("Tap me") .onTapGesture { print("View tapped!") } } }

In the above example, a text view is created with the text "Tap me", and the onTapGesture modifier is used to specify a closure that prints "View tapped!" to the console when the view is tapped. You can replace the print statement with any code that you want to be executed when the view is tapped.

How to add a binding to a SwiftUI view?

To add a binding to a SwiftUI view, you can follow these steps:

  1. Define a property in your view that will hold the bound value. This property should be marked with the @State, @Binding, or @ObservedObject property wrapper, depending on the type of binding you want to use.
  2. Pass the binding as a parameter to the view when you create an instance of it. This binding should be a reference to the property you defined in the previous step.
  3. Use the bound value in your view by accessing the property you defined in step 1.

Here is an example of how to add a @State binding to a SwiftUI view:

import SwiftUI

struct ContentView: View { @State private var toggleValue = false

var body: some View {
    Toggle(isOn: $toggleValue) {
        Text("Toggle")
    }
}

}

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

In this example, we have defined a @State property named toggleValue that holds the value of a toggle switch. We then pass this property as a binding with the $ prefix to the Toggle view, allowing us to update the state of the toggle switch when the user interacts with it.

What is the @GestureState property wrapper in SwiftUI?

The @GestureState property wrapper in SwiftUI is used to store transient state related to a gesture. It allows you to keep track of the current state of a gesture, such as its start location, translation, or scale factor, and update it dynamically as the user interacts with the gesture.

The @GestureState property wrapper works in conjunction with the gesture modifiers in SwiftUI, such as onTapGesture, onDragGesture, or onLongPressGesture. When you attach a gesture modifier to a view and use @GestureState to store the gesture's state, SwiftUI automatically updates the @GestureState property as the user interacts with the gesture.

By using @GestureState, you can create more complex and interactive user interfaces in SwiftUI that respond to user input and update dynamically based on the user's gestures.

What is the @ObservedObject property wrapper in SwiftUI?

The @ObservedObject property wrapper in SwiftUI is used to declare a property that represents an external object that the view needs to observe for changes. When a property is declared with @ObservedObject, SwiftUI will monitor the external object for changes, and the view will be automatically updated whenever the object changes.

This is commonly used to observe changes in instances of classes that conform to the ObservableObject protocol. By using @ObservedObject, you can easily update the view whenever the observed object's data changes, without having to manually trigger the update.

How to add a custom view modifier to a SwiftUI view?

To add a custom view modifier to a SwiftUI view, you can create a new struct that conforms to the ViewModifier protocol and implement the desired styling or behavior for your view. Here is an example of how you can create a custom view modifier:

  1. Create a new struct that conforms to the ViewModifier protocol:

struct CustomModifier: ViewModifier { func body(content: Content) -> some View { content .foregroundColor(.red) .font(.title) .padding() } }

  1. Use the custom view modifier in your SwiftUI view by applying it to the desired view using the modifier method:

struct ContentView: View { var body: some View { Text("Hello, World!") .modifier(CustomModifier()) } }

By using the modifier method, you can apply the custom view modifier to any view in your SwiftUI application to enhance its appearance or behavior.

How to add a state object to a SwiftUI view?

To add a state object to a SwiftUI view, you can use the @State property wrapper. Here's an example of how you can add a state object to a SwiftUI view:

import SwiftUI

struct ContentView: View { @State private var count = 0

var body: some View {
    VStack {
        Text("Count: \\(count)")
            .font(.largeTitle)
        
        Button(action: {
            self.count += 1
        }) {
            Text("Increment")
        }
    }
}

}

In the above example, the @State property wrapper is used to create a state property called count which keeps track of the current count. The Text view displays the current value of count, and the Button view increments the count value when tapped.

You can also use the @State property wrapper with other types such as String, Bool, or custom types. Just like in the example above, declare a new property using @State and update its value as needed within the view.

What is the @FetchRequestSectionFooter property wrapper in SwiftUI?

The @FetchRequestSectionFooter property wrapper in SwiftUI is used to provide a footer view for each section in a fetch request in a SwiftUI List or ForEach view. This property wrapper allows you to specify a view that will be displayed as the footer for each section of data fetched from a Core Data fetch request. This can be useful for displaying additional information or actions related to each section of data in a list or collection view.