Swift3无Storyboard下按钮点击跳转视图的编程实现方法问询
Hey there! Let's walk through exactly how to handle button taps and view transitions in Swift 3 when building your UI entirely in code. I'll break this down step by step for you:
You're right that navigation bar buttons use the target/action pattern, and regular UIButtons work almost the same way—you just use the addTarget method to link the button to a selector function. Here's how to implement it:
First, when you create your login/register button in code:
// 示例:创建登录按钮 let loginButton = UIButton(type: .system) loginButton.setTitle("Login", for: .normal) // 绑定点击事件:触发handleLoginTap方法,响应touchUpInside事件 loginButton.addTarget(self, action: #selector(handleLoginTap), for: .touchUpInside) // 记得设置按钮的布局(frame或AutoLayout) loginButton.frame = CGRect(x: 50, y: 300, width: 270, height: 50) self.view.addSubview(loginButton)
Then implement the selector method (make sure to mark it with @objc since Swift 3 requires this for Objective-C interoperability):
@objc func handleLoginTap() { // 这里就是你要写跳转逻辑的地方 print("Login button tapped!") }
This is the core pattern for any regular button interaction—just like the navigation bar button, but you explicitly attach the target/action to the button instance.
Once you've got the button tap working, you have two primary ways to navigate to a new view controller:
Option A: Present a Modal View Controller
Use this when you want to present the new screen as an overlay (like a signup sheet, or a full-screen main app after login). It doesn't require a navigation stack:
@objc func handleLoginTap() { // 1. 实例化目标视图控制器 let mainViewController = MainViewController() // 2. 可选:设置模态展示样式(比如.fullScreen、.pageSheet等) mainViewController.modalPresentationStyle = .fullScreen // 3. 执行模态跳转 self.present(mainViewController, animated: true, completion: nil) }
To dismiss this modal later (e.g., from the MainViewController), call:
self.dismiss(animated: true, completion: nil)
Option B: Push to a New View Controller (Requires UINavigationController)
If your login screen is embedded in a UINavigationController, you can push the new controller onto the navigation stack—this adds a back button automatically for easy navigation back.
First, make sure your login controller is wrapped in a navigation controller when you set up your app's root (usually in AppDelegate or SceneDelegate):
let loginVC = LoginViewController() let navigationController = UINavigationController(rootViewController: loginVC) window?.rootViewController = navigationController window?.makeKeyAndVisible()
Then in your button tap handler:
@objc func handleLoginTap() { let mainViewController = MainViewController() // Push the new controller onto the navigation stack self.navigationController?.pushViewController(mainViewController, animated: true) }
To go back, you can either use the automatic back button, or call programmatically:
self.navigationController?.popViewController(animated: true)
Since you're using pure code, this might not be necessary for your case, but I'll include it just in case you ever need to work with Storyboards later. In Swift 3, you can trigger a segue programmatically by:
- Giving your segue a unique identifier in Storyboard (e.g., "LoginToMain")
- Calling
performSeguein your button tap handler:
@objc func handleLoginTap() { self.performSegue(withIdentifier: "LoginToMain", sender: self) }
- Optional: Override
prepare(for:sender:)to pass data to the destination controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "LoginToMain" { guard let mainVC = segue.destination as? MainViewController else { return } // Pass data here, e.g.: // mainVC.user = currentLoggedInUser } }
内容的提问来源于stack exchange,提问作者CJ90

