Use UIAlertController with the .actionSheet style when people pick one of several actions, add a .cancel action, and always set popoverPresentationController.sourceView and sourceRect (or sourceItem for a bar button). On iPad the sheet appears as a popover and needs that anchor. For a short list of choices on a button, a UIMenu with showsMenuAsPrimaryAction is often the better fit.
SWIFT TIPS · UIKIT
First posted on April 15, 2018 for Swift 4, as "Button Alerts With UIAlertController In Swift 4". Rewritten on November 17, 2026. The Swift code below was type-checked on a Mac with Xcode 26.2 against the iOS 26.2 SDK, in Swift 5 and Swift 6 language modes, with iOS 15 as the deployment target. It wasn't run on a device.
In 2018 I needed a button that offered two choices: upload a photo, or see an example photo first. The post showed the action sheet I used for that, plus a plain alert for a failed login. The alert part has its own rewritten post now, linked at the end. This one is about the action sheet, because that's where the code from 2018 can still break an app today, and the break only shows up on iPad.
An action sheet that works on iPhone and iPad
import UIKit
final class ProfileViewController: UIViewController {
@IBOutlet private weak var photoButton: UIButton!
@IBAction func photoTapped(_ sender: UIButton) {
let sheet = UIAlertController(title: "Profile photo", message: nil, preferredStyle: .actionSheet)
sheet.addAction(UIAlertAction(title: "Take Photo", style: .default) { [weak self] _ in
self?.openCamera()
})
sheet.addAction(UIAlertAction(title: "Choose From Library", style: .default) { [weak self] _ in
self?.openLibrary()
})
sheet.addAction(UIAlertAction(title: "Remove Photo", style: .destructive) { [weak self] _ in
self?.removePhoto()
})
sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel))
// On iPad the sheet becomes a popover and needs something to point at
if let popover = sheet.popoverPresentationController {
popover.sourceView = sender
popover.sourceRect = sender.bounds
}
present(sheet, animated: true)
}
@objc func moreTapped(_ item: UIBarButtonItem) {
let sheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
sheet.addAction(UIAlertAction(title: "Share", style: .default))
sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel))
if #available(iOS 16.0, *) {
sheet.popoverPresentationController?.sourceItem = item
} else {
sheet.popoverPresentationController?.barButtonItem = item
}
present(sheet, animated: true)
}
// iOS 14 and later: a menu straight from the button, no sheet at all
func configurePhotoMenu() {
photoButton.menu = UIMenu(title: "Profile photo", children: [
UIAction(title: "Take Photo", image: UIImage(systemName: "camera")) { [weak self] _ in self?.openCamera() },
UIAction(title: "Choose From Library", image: UIImage(systemName: "photo")) { [weak self] _ in self?.openLibrary() },
UIAction(title: "Remove Photo", image: UIImage(systemName: "trash"), attributes: .destructive) { [weak self] _ in self?.removePhoto() },
])
photoButton.showsMenuAsPrimaryAction = true
}
private func openCamera() {}
private func openLibrary() {}
private func removePhoto() {}
}
The photoTapped method is the core of it, and it follows the same order every time:
- Create the controller with
preferredStyle: .actionSheet. A title is optional. If the choices explain themselves, leave both title and message out. - Add the actions. Use
.destructivefor anything that deletes or can't be undone, so iOS shows it in red. - Add one
.cancelaction. UIKit puts it apart from the others, at the bottom on iPhone. - Give the popover an anchor, then present.
Why the anchor matters
On iPhone an action sheet slides up from the bottom of the screen. On iPad, and in a regular-width window, UIKit shows it as a popover that points at something on screen. A popover needs to know what to point at. The sheet's popoverPresentationController exists for exactly this: set its sourceView to the button that was tapped and its sourceRect to that button's bounds.
Leave it out and it goes wrong only on iPad: UIKit refuses to present the popover without an anchor and the app stops with an exception that names the missing source view. I didn't trigger that crash for this rewrite. If your app runs on iPad, test every action sheet there at least once.
For a sheet that opens from a navigation bar button, use sourceItem, which iOS 16 added. Apple's documentation notes a change in iOS 26: the popover now animates out of the bar button and takes its place until the user picks something, instead of only pointing at it with an arrow. The moreTapped method falls back to the older barButtonItem property on iOS 15.
The if let popover check is safe on iPhone. The property is there on both, and setting the anchor doesn't change how the sheet looks on a phone.
Weak self in the action handlers
The handlers capture [weak self]. The sheet only lives while it's on screen, so a strong capture wouldn't leak memory here. I keep the weak capture anyway because the work in those handlers often starts something that takes time, like opening the camera. If the screen is gone by then, nothing should run.
When a menu is the better choice
Since iOS 14 a button can show a menu directly. configurePhotoMenu builds the same three choices as a UIMenu and sets showsMenuAsPrimaryAction, so a single tap opens it. Apple's documentation says the menu then appears on touch down.
I reach for a menu when the choices belong to one button and none of them needs an explanation. A menu stays next to the button, works the same on iPhone and iPad, and needs no anchor code. An action sheet is still the better fit when you want a title or message above the choices, or to confirm something destructive the user just started.
A quick check for your project
- Search the project for
.actionSheet. For each result, check thatsourceView,sourceItemorbarButtonItemis set beforepresent. - Run the app on an iPad simulator and open each of those sheets once. One missing anchor is enough to crash.
- Look for sheets with a single action plus Cancel. Those are usually easier as a plain button or a menu.
What the 2018 version said
The original action sheet had Upload, an example photo option that presented ContohFotoViewController, and Cancel. It had no popover anchor, which was fine in an iPhone-only app. The second half of the post showed a login-failed alert, which is now covered in the rewritten alert post.
Keep reading on JR Devhub
Comments