登录按钮点击无法执行toLogin segue的问题求助
Hey there! Let’s walk through why your login button might only be changing color and not triggering the toLogin segue (or even firing your print statement)—we’ll cover all the common pitfalls that trip up new iOS developers:
1. Double-Check Your Button’s Event Connections
First up: make sure your button is actually wired to trigger the segue (or your IBAction, if you’re using one).
- If you dragged the segue directly from the button to the target controller:
- Open the Storyboard, select your login button, then open the Connections Inspector (the arrow icon in the right sidebar). Look for the
Touch Up Insideevent—there should be a connection listed that links to thetoLoginsegue. If not, re-drag the segue from the button to the target controller, making sure you select the correct segue type (like "Push" or "Present Modally") and set the identifier totoLoginin the Attributes Inspector.
- Open the Storyboard, select your login button, then open the Connections Inspector (the arrow icon in the right sidebar). Look for the
- If you’re using an IBAction to call
performSegue(withIdentifier:sender:):- Confirm the
Touch Up Insideevent is connected to your IBAction method in the Connections Inspector. Also, double-check your code to make sure you’re using the exact identifier:@IBAction func loginTapped(_ sender: UIButton) { print("Login button pressed!") performSegue(withIdentifier: "toLogin", sender: sender) } - Typos in the identifier (like "ToLogin" instead of "toLogin") are super easy to miss!
- Confirm the
2. Make Sure the Button Isn’t Blocked
Sometimes a transparent UIView or another control sitting on top of your button can "steal" the tap event, even if you can see the button.
- In the Storyboard, select your login button, then go to
Editor > Arrange > Bring to Frontto move it to the top of the view hierarchy. - Check any overlapping views and make sure their
isUserInteractionEnabledproperty is set tofalse(you can find this in the Attributes Inspector). If a view on top has user interaction enabled, it’ll intercept taps before they reach your button.
3. Verify the Button Is Enabled
A disabled button will still change color when tapped, but it won’t trigger any actions.
- In the Storyboard’s Attributes Inspector for the button, make sure the "Enabled" checkbox is checked.
- Check your code to see if you accidentally set
loginButton.isEnabled = falsesomewhere (like inviewDidLoador a validation method).
4. Check for a shouldPerformSegue Block
If you’ve overridden the shouldPerformSegue(withIdentifier:sender:) method in your view controller, it might be returning false for your toLogin segue, blocking it from running.
- Search your view controller code for this method. If you see it, make sure it returns
truewhen the identifier matchestoLogin:override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { if identifier == "toLogin" { return true // Allow the segue to run } return super.shouldPerformSegue(withIdentifier: identifier, sender: sender) } - If you don’t need this method at all, just delete it—it’s not required by default.
5. Confirm the Segue Type Is Valid
If you’re using a "Push" segue, your current view controller must be embedded in a UINavigationController for the push to work. If it’s not, the segue will silently fail.
- Check if your initial view controller is wrapped in a Navigation Controller (look for the navigation bar at the top). If not, select your view controller, go to
Editor > Embed In > Navigation Controllerto add it.
Start with these checks—most of the time, the issue is a simple connection mistake or a blocked button. Let me know if any of these fix your problem!
内容的提问来源于stack exchange,提问作者Oliver Silverstein

