Swift alert view with OK and Cancel which button tapped?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In modern iOS code, the normal way to show an alert with OK and Cancel is UIAlertController plus two UIAlertAction handlers. You do not inspect some separate “which button was tapped” property later. Instead, you attach the code for each button directly to the action that represents it. That makes the user's choice explicit and easy to handle.
Use UIAlertController with Separate Action Handlers
A standard alert setup looks like this:
When the user taps OK, the okAction closure runs. When the user taps Cancel, the cancelAction closure runs. That is the canonical answer.
The Closure Is the Button Callback
Each UIAlertAction receives a handler closure. That closure is where you react to the button tap.
This means you do not usually write code like:
- show alert
- wait
- ask later which button won
Instead, the alert is event-driven. Each action carries its own response logic.
This model fits UIKit's general style and avoids global state just to remember which alert button was chosen.
If You Need Shared Logic, Use a Helper Method
Sometimes both buttons need to call into shared application logic. In that case, you can route the alert actions to helper methods or enums instead of putting all logic inline.
This pattern is useful when the alert result feeds into a larger control flow.
Alert Style and Button Semantics Matter
UIKit also distinguishes button styles:
- '
.defaultfor ordinary confirmation' - '
.cancelfor cancellation' - '
.destructivefor dangerous actions'
The style affects both appearance and user expectations. So even though “which button was tapped” is the direct question, you should also choose styles that match the meaning of the action.
For example, deleting a record often deserves a destructive action rather than a plain default action.
Present the Alert from the Correct View Controller
The alert must be presented from a view controller that is currently in the view hierarchy. If you call present too early or from an inactive controller, the alert may fail to appear or produce a runtime warning.
That means the correctness of the button handler is only part of the problem. The alert must also be presented from the right place in the UIKit life cycle.
Older UIAlertView Code Is Obsolete
Older Swift and Objective-C examples on the internet may use UIAlertView with delegate callbacks. That API is deprecated. If you see examples that talk about alert view tags or delegate methods to determine which button was tapped, treat them as legacy code, not modern UIKit guidance.
The modern answer is UIAlertController plus action handlers.
Common Pitfalls
The most common mistake is looking for a separate “selected button” return value after presenting the alert. UIAlertController does not work that way.
Another mistake is doing too much work inline in the action closures. If the result handling becomes large, move it into helper methods.
Developers also copy obsolete UIAlertView examples and then get confused when modern Swift code does not follow the same delegate pattern.
Summary
- In modern Swift, detect
OKversusCancelby attaching separate handlers toUIAlertActionobjects. - Each button runs its own closure when tapped.
- Use helper methods or enums if the alert result feeds into larger logic.
- Present the alert from a valid visible view controller.
- Prefer
UIAlertController;UIAlertViewpatterns are legacy and should not be used in new code.
Related reading
- Swift and mutating struct
- Swift apply .uppercaseString to only the first letter of a string
- Swift apply .uppercaseString to only the first letter of a string
- Swift Array - Check if an index exists
- Swift Async let with loop
- Swift Beta performance sorting arrays
- Swift Beta performance sorting arrays
- Swift Bridging Header import issue
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.