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

SwiftUI页面导航数据传递异常问题求助

问题根源

你把NavigationLink嵌套在Button里的写法逻辑冲突:点击时NavigationLink会立刻触发页面跳转,而Button里的selectedGroupType赋值操作还没执行完成,导致目标页面拿到的始终是selectedGroupType的初始空值。

解决方案

下面提供两种符合SwiftUI导航规范的修复方式:

方式一:用NavigationLink的tag+selection绑定(推荐)

通过绑定选中项来触发导航,确保跳转时数据已经正确赋值:

struct SelectGroupTypeView: View {
    @State private var selectedGroup: GroupType?
    private var groups: [GroupType] = [
        GroupType(type: "Festivals", image: "Festivals"),
        GroupType(type: "Parties", image: "Parties"),
        GroupType(type: "Stores/Malls", image: "Stores"),
        GroupType(type: "Restaurants", image: "Restaurants"),
        GroupType(type: "Gyms and Excersize", image: "Excersize"),
        GroupType(type: "Education/Study Groups", image: "StudyGroup")
    ]
    
    var body: some View {
        // iOS16+用NavigationStack,iOS15及以下用NavigationView
        NavigationStack {
            VStack{
                ForEach(0..<groups.count/2) { row in
                    HStack {
                        ForEach(0..<2) { column in
                            let index = row * 2 + column
                            if index < groups.count {
                                let group = groups[index]
                                NavigationLink(
                                    destination: GroupCreationView(groupType: group),
                                    tag: group,
                                    selection: $selectedGroup,
                                    label: {
                                        VStack{
                                            Image(group.image ?? "")
                                                .resizable()
                                                .scaledToFit()
                                            Text(group.type ?? "")
                                        }
                                        .frame(maxWidth: .infinity)
                                    }
                                )
                                .onTapGesture {
                                    selectedGroup = group
                                    print(group)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

对应的接收页面(无需双向绑定时用普通属性接收):

struct GroupCreationView: View {
    let groupType: GroupType
    @State var groupTypeString: String = ""
    
    var body: some View {
        VStack{
            Image(groupType.image ?? "")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(maxWidth: 300)
            Text(groupType.type ?? "")
        }
    }
}

方式二:直接传递当前项到目标页面

如果不需要后续修改数据回传,也可以直接把当前循环的group对象传入目标页面,无需额外的@State变量:

// 发起页面修改部分
ForEach(0..<2) { column in
    let index = row * 2 + column
    if index < groups.count {
        let group = groups[index]
        NavigationLink(destination: GroupCreationView(groupType: group)) {
            VStack{
                Image(group.image ?? "")
                    .resizable()
                    .scaledToFit()
                Text(group.type ?? "")
            }
            .frame(maxWidth: .infinity)
        }
        .onTapGesture {
            print(group)
        }
    }
}

接收页面同方式一的代码即可。

内容的提问来源于stack exchange,提问作者DevAcc

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 05:45:19