Swift performSegueWithIdentifier not working
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
performSegue(withIdentifier:sender:) fails silently or crashes when the segue identifier does not match the storyboard, the segue is not connected to the correct view controller, or the method is called before the view is loaded. The most common causes are a typo in the identifier string, a missing segue connection in Interface Builder, or calling the method from a view controller that is not in the navigation hierarchy. Check the storyboard identifier, verify the source view controller, and use prepare(for:sender:) for data passing.
The Basic Call
This calls the segue named "showDetail" from the current view controller. If it fails, the error is typically one of the issues below.
Cause 1: Wrong Segue Identifier
The identifier is case-sensitive. A single character mismatch causes a runtime crash with the message has no segue with identifier.
Cause 2: Segue Not Connected in Storyboard
If the segue line is not visible between the two view controllers in the storyboard, the connection is missing. Control-drag from the source to the destination to create it.
Cause 3: Calling Before View Is Loaded
A segue can only be performed when the view controller is part of the view hierarchy. Call performSegue in viewDidAppear or in response to user actions, not in init or viewDidLoad.
Cause 4: Wrong Source View Controller
Each segue belongs to a specific source view controller. You can only trigger it from that controller.
Passing Data with prepare(for:sender:)
prepare(for:sender:) is called automatically before the segue transition. Use it to configure the destination view controller.
Modern Alternative: Programmatic Navigation
Programmatic navigation avoids segue identifier issues entirely and makes data passing explicit. Many modern iOS codebases prefer this over storyboard segues.
Debugging Tips
Common Pitfalls
- Segue identifier typo: The identifier string is case-sensitive and must exactly match the storyboard. Use constants or enums instead of string literals to catch typos at compile time.
- Calling
performSeguefromviewDidLoad: The view controller may not be in the window hierarchy yet. The segue may fail silently or crash. UseviewDidAppearor trigger from user actions (button taps, table cell selection). - Segue connected to the wrong view controller: In complex storyboards, it is easy to connect a segue from the wrong source. Verify in the Connections Inspector that the segue appears under the correct view controller's "Triggered Segues."
- Not implementing
prepare(for:sender:)for data passing: Withoutprepare, the destination view controller receives no data from the source. The segue still transitions, but the destination appears empty or with default values. - Using deprecated
performSegueWithIdentifier:sender:syntax: The Objective-C method nameperformSegueWithIdentifier:sender:was renamed toperformSegue(withIdentifier:sender:)in Swift 3+. Using the old name causes a compile error in modern Swift.
Summary
- Verify the segue identifier matches exactly (case-sensitive) between code and storyboard
- Ensure the segue is connected from the correct source view controller in Interface Builder
- Call
performSegueonly after the view is in the hierarchy (viewDidAppear, notviewDidLoad) - Use
prepare(for:sender:)to pass data to the destination view controller - Use constants or enums for segue identifiers to prevent typos
- Consider programmatic navigation (
pushViewController) as a more maintainable alternative to storyboard segues
Related reading
- Swift performSelectorwithObjectafterDelay is unavailable
- Swift performSelectorwithObjectafterDelay is unavailable
- Swift pods cannot yet be integrated as static libraries FirebaseCoreInternal-library
- Swift print vs println vs NSLog
- Switching threads within PDB
- Symbol not found kUTTypeImage
- Swift programmatically navigate to another view controller/scene
- Swift Programming getter/setter in stored property
.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.