iOS开发:UIButton跳转UIViewController的Push Segue问题求助
Hey there, fellow iOS newbie! I totally get how frustrating it can be when segues don't work as expected—let's walk through the most common fixes step by step, with visual guides to help you out.
1. Make Sure Your Initial View Controller is Embedded in a Navigation Controller
Push segues only work within a UINavigationController stack—if your starting VC isn't part of a navigation stack, the push action will fail silently or throw an error. Here's how to fix it:
- Open your
Main.storyboardand select your initial view controller (the one with the UIButton). - Go to the top menu bar:
Editor > Embed In > Navigation Controller. - You'll now see a navigation controller wrapping your initial VC, like this:
Initial VC Embedded in Navigation Controller
2. Verify Your Segue Connection & Type
Double-check that you set up the segue correctly:
- Select your UIButton, then open the Connections Inspector (the right-most tab with an arrow icon).
- Look under "Triggered Segues"—you should see your segue listed, with its type set to
Push.
Button Connections Inspector Showing Push Segue - If it's set to something else (like Modal), delete the existing segue and re-drag the connection, making sure to select
Pushfrom the Action Segue options.
3. Confirm Target View Controller Class Binding
It's easy to forget to link your storyboard VC to its corresponding Swift/Objective-C class, which can cause segue failures:
- Select your target view controller in the storyboard.
- Open the Identity Inspector (the tab with a name tag icon).
- In the "Custom Class" section, make sure the
Classfield is set to your actual view controller subclass (e.g.,DetailViewController).
Target VC Identity Inspector Class Binding
4. Check for Blocking Segue Logic in Code
If you've added a shouldPerformSegue(withIdentifier:sender:) method in your initial view controller, it might be returning false and blocking the segue:
- Open your initial VC's code file, and look for this method:
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { // Ensure this returns true (or adjust logic to allow the segue when needed) return true } - If the method returns
falseby default, change it totrueto let the segue execute.
If none of these fixes work, could you share a screenshot of the error message you're seeing? That will help narrow down the exact issue!
内容的提问来源于stack exchange,提问作者Pascal Okyere Adomako

