SwiftUI中Segue导航:能否创建Segue并使用prepare方法实现页面传值?
Hey there! Great question—let’s clear this up right away.
No, you can’t use Segues or the prepare(for segue: UIStoryboardSegue, sender: Any?) method natively in SwiftUI. Segues are a UIKit/Storyboard-specific navigation mechanism, and SwiftUI uses a completely different, declarative approach to navigation and data passing.
SwiftUI is built around a declarative syntax where you define your UI based on state, rather than the imperative "trigger a segue" pattern from UIKit. Storyboards (and thus segues) aren’t part of SwiftUI’s core workflow—instead, SwiftUI uses navigation components like NavigationStack (or NavigationView for older iOS versions) and NavigationLink to handle screen transitions.
Here are two common, idiomatic ways to pass data between SwiftUI views:
1. Directly Pass Data via View Initializers
The simplest way is to pass your data directly when you initialize the destination view. This works great for one-way data flow:
// Destination view that accepts data struct DetailView: View { let userMessage: String var body: some View { VStack(spacing: 20) { Text("Data Received!") .font(.title) Text(userMessage) .font(.headline) } } } // Source view that triggers the navigation struct ContentView: View { private let messageToSend = "Hey, this data came from the main view!" var body: some View { NavigationStack { VStack { Text("Main View") .font(.largeTitle) NavigationLink("Go to Detail") { // Pass data directly to the detail view's initializer DetailView(userMessage: messageToSend) } .buttonStyle(.borderedProminent) } .padding() } } }
2. Share State with ObservableObject
For more complex scenarios where you need two-way data binding or shared state across multiple views, use an ObservableObject:
// Observable model to hold shared state class SharedData: ObservableObject { @Published var userName: String = "Anonymous" } // Source view that updates the shared state struct ContentView: View { // Create the model once and manage its lifecycle @StateObject private var sharedData = SharedData() var body: some View { NavigationStack { VStack(spacing: 20) { Text("Main View") .font(.largeTitle) TextField("Enter your name", text: $sharedData.userName) .textFieldStyle(.roundedBorder) .padding(.horizontal) NavigationLink("Go to Detail") { // Pass the shared model to the detail view DetailView(sharedData: sharedData) } .buttonStyle(.borderedProminent) } .padding() } } } // Destination view that reads/writes to the shared state struct DetailView: View { // Observe changes to the shared model @ObservedObject var sharedData: SharedData var body: some View { VStack(spacing: 20) { Text("Detail View") .font(.title) Text("Hello, \(sharedData.userName)!") .font(.headline) TextField("Update name", text: $sharedData.userName) .textFieldStyle(.roundedBorder) .padding(.horizontal) } .padding() } }
If you’re working in a hybrid app where you have SwiftUI views embedded in UIKit ViewControllers (or vice versa), you might still use segues within the UIKit part. But for pure SwiftUI navigation, stick to the patterns above.
内容的提问来源于stack exchange,提问作者Anupam Mishra

