I want an Alert with one button that runs some code. I do not want there to be a cancel button. I've only seen a way to have two buttons, with primaryButton and secondaryButton. Is there a way to do something like this?
Create an Alert with just one action button
3.4k Views Asked by Evan93 At
1
There are 1 best solutions below
Related Questions in SWIFT
- Overlapping UICollectionView in storyboard
- Cannot pod spec lint because of undeclared type errors
- Swift code with multiple NSDateFormatter - optimization
- How do I add multiple in app purchases in Swift Spritekit?
- cellForRowAtIndexPath and prepareForSegue return different label colors
- Getting this message in my console in xcode "Ignoring restoreCompletedTransactionsWithApplicationUsername: because already restoring transactions"?
- Change background of an Accessory View in a UITableViewCell
- fade in an bounce animation subview
- Create a PFObject and PFRelation after PFUser Sign Up
- Swift 2 - Pattern matching in "if"
- How do I give inputs through NSURL
- How do I add custom cells to TableView in Swift?
- UIWebView not loading URL in simulator
- Compiler complains that 'Expression resolved to unused function' when removing index in array of functions
- Cast from 'Int?' to unrelated type 'NSNumber' always fails
Related Questions in SWIFTUI
- Ternary operator inside NavigationLink SwiftUI
- Use ViewRouter in SwiftUI Project in Xcode 12
- SwiftUI Navigation View - Handling Size Classes over multiple views
- Is there any way to create a new Gesture in SwiftUI?
- SwiftUI extension to display Image in Alert()
- Get changes on button tapped using Combine and SwiftUI
- Does `ShapeStyle` inherit from `View`?
- Nested transitions / animations in SwiftUI
- How do I set an image in a cell class from a ImagePicker Controller
- How to disable minimum value of 0 in Slider?
- SwiftUI NavigationView issue with Parent-Child-Grandchild scenario when attempting grandchild previous next navigation
- SwiftUI strange tabbar behaviour with groups
- Picker in SwiftUI 2 .onChange() does not change UINavigationBar.appearance()
- SwiftUI Change Form Background
- SwiftUI image picker, url image from camera
Related Questions in ACTION
- Calling controller action from action in component
- Why this submit action dont work with php isset?
- Making an HTML Jigsaw Puzzle in Flash CC
- specify type of delegate parameter
- Cocos2D 2.x: Running CCWave action makes sprite disappear
- Render outcome of "action" attribute?
- one form two actions with javascript
- Removing a Wordpress action added from a Plugin and than adding a new action in place of that
- How to add action for custom form in wordpress?
- Java: Need pressing enter to trigger action and actionlistener
- Java actionPerformed questions
- Random Line Appearing on Moving Texture Node
- Understanding touch system
- How to subclass an UIButton to pass multiple NSString parameters via addTarget:action:forControlEvents
- Java JButton.setAction(...) null's button text
Related Questions in ALERT
- Can't see how to disable an AlertDialog Box
- Unable to send email through PHP with alert message as notification linked with jQuery
- how to realize an alert that disappears after some seconds
- Detect Country then show message?
- Javascript: display an alert when a form is checked?
- Alerting framework for incoming traffic
- Excel VBA - Value & MsgBox
- Make an alert only appear when app is opened
- Using onbeforeunload to create a custom confirmation warning?
- Javascript return in function in object won't show alert box
- Javascript alert box blocks sound object
- How can I make this script to pop up instead of alert
- Alert Dialogue in Android-beginners
- Running function inside document.write which document.write inside window.open
- Value change Alert VBA
Related Questions in SWIFTUI-ALERT
- SwiftUI: Custom .sheet modifier need to attach to top most window of app instead of View
- SwiftUI Alert setting presentation condition true in it's button action closure not showing the alert again
- How do I make an toggle button inside an alert in swiftUI
- Create an Alert with just one action button
- Swift - Thread 1: EXC_BAD_INSTRUCTION when deleting object from Coredata
- SwiftUI alert is not displaying when navigating to screen second time
- sheet can not be dismissed after an alert is displayed
- SwiftUI - show Alert from Button inside of ToolbarItem
- Cannot convert value of type 'AlertViewPOP' to closure result type 'Alert'
- SwiftUI: Alert that does not close when clicking on button
- presenting a confirm alert after an alert with swiftui not working
- How to present an Alert in SwiftUI with no buttons
- Issue with SwiftUI displaying Alert – Will not display alert box
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
When creating an Alert in SwiftUI the documentation is a great place to look to see what is available to you.
In the Creating an Alert section we see the following:
As the documentation states, to create an Alert with one button you choose the second option.
There are four different button types that you can use in an Alert, see here.
So depending on what you want your button to do there are plenty of options to choose from. Notice that performing an action on a button is optional. So basically you can have your button do something or nothing if tapped.
Alerts with no action
These three alerts are identical in what they produce.
Because action is optional and its default value is nil we can either leave out, pass nil, or we can pass an empty closure. The first option is what I would go for if I wasn't performing an action.
Alerts with an action
If we wanted an action to be performed we just have to include it the action parameter. We can either write all our code in the closure that we pass or we can write it as a function.
This alert has the code that should be run when action is tapped contained with the closure. This is ok if you have a single line of code to run, but it can start to clutter up your view if you have multiple lines of code.
This Alert's action relies on a function that has been declared in the ContentView. This means that a function that it quite complicated won't clutter up your view code.