关于UIButton触发Segue的代码实现问题咨询
Hey, let's sort out your UIButton segue issue. First, let's spot the problems in your current code, then walk through two solid solutions:
问题分析
Looking at your code, there are a few key issues preventing the segue from working correctly:
- Your
transfermethod is instantiatingViewController()(the same controller you're currently on) — this would just create an infinite loop of opening the same page instead of your target view controller. - The selector in
addTargetis incomplete (#selector(tran...) and points to a method that isn't marked with@objc(required for selectors in Objective-C runtime). - You forgot to add the button to your view with
view.addSubview(btn1), so the button won't even appear on screen!
解决方案1:纯代码手动跳转(无需Storyboard Segue)
If you're working entirely with code (no Storyboard), here's the fixed and complete code:
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Create and configure the button let btn1 = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 30)) btn1.backgroundColor = .systemBlue btn1.setTitle("跳转", for: .normal) btn1.setTitleColor(.white, for: .normal) // Link the button to our jump method btn1.addTarget(self, action: #selector(transferToTargetVC), for: .touchUpInside) // Don't forget to add the button to the view! view.addSubview(btn1) } // Mark with @objc so it can be recognized as a selector @objc func transferToTargetVC() { // Replace this with your actual target view controller class let targetVC = SecondViewController() // Choose the right navigation method: // 1. If using a UINavigationController: // self.navigationController?.pushViewController(targetVC, animated: true) // 2. For modal presentation (like a popup): self.present(targetVC, animated: true, completion: nil) } } // Example target view controller (replace with your own) class SecondViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white title = "目标页面" // Add any setup for your target VC here } }
Key notes for this approach:
- Always instantiate your actual target view controller (not the current one)
- Use
pushViewControllerif your controller is embedded in aUINavigationController, otherwise usepresentfor modal transitions - Make sure to add the button to your view hierarchy with
view.addSubview(btn1)
解决方案2:使用Storyboard Segue
If you're using Storyboards to manage your view controllers, follow these steps:
- In your Storyboard, drag from your
ViewControllerto your target view controller to create a Segue. Select the Segue, go to the Attributes Inspector, and set its Identifier (e.g.,goToTargetVC). - Use this code to trigger the Segue from your button:
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let btn1 = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 30)) btn1.backgroundColor = .systemBlue btn1.setTitle("跳转", for: .normal) btn1.setTitleColor(.white, for: .normal) btn1.addTarget(self, action: #selector(triggerSegue), for: .touchUpInside) view.addSubview(btn1) } @objc func triggerSegue() { // Trigger the Segue using the identifier you set in Storyboard performSegue(withIdentifier: "goToTargetVC", sender: self) } // Optional: Pass data to the target view controller override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "goToTargetVC" { // Cast the destination to your target VC class guard let targetVC = segue.destination as? SecondViewController else { return } // Pass data here, e.g.: // targetVC.userData = "Hello from the first screen!" } } }
Key notes for this approach:
- Double-check that the Segue identifier in code matches exactly what you set in Storyboard
- Use
prepare(for:sender:)to pass any necessary data to the target view controller before the segue happens
内容的提问来源于stack exchange,提问作者user9620161
相关产品推荐
相关产品推荐

