You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中Segue导航:能否创建Segue并使用prepare方法实现页面传值?

Hey there! Great question—let’s clear this up right away.

Short Answer

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.

Why This Doesn’t Apply to SwiftUI

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.

How to Pass Data Between SwiftUI Views Instead

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()
    }
}
Edge Case: SwiftUI + UIKit Mixing

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:27:06