更新结构时 SwiftUI 导航不起作用 [英] SwiftUI navigation doesn't work when updating struct

查看:20
本文介绍了更新结构时 SwiftUI 导航不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构列表.点击按钮将调用结构上的变异函数,然后导航.

I have a list of structs. Tapping a button will call a mutating function on a struct, then navigate.

但是导航不会触发.如果删除了对变异函数 self.logins[index].updateLastLogin() 的调用,它将再次开始工作.为什么?

The navigation however won't trigger. It'll start working again if the call to the mutating function self.logins[index].updateLastLogin() is removed. Why?

要重现,请将以下内容粘贴到一个空的 SwiftUI 项目中:

To reproduce, paste the following in an empty SwiftUI project:

struct Login: Identifiable, Hashable {
    let id = UUID()
    let name: String
    var lastLogin: Date?

    mutating func updateLastLogin() {
        self.lastLogin = Date()
    }
}

struct ContentView: View {
    @State private var logins = ["Zaphod", "Arthur", "Ford", "Marvin", "Trillian"].map { Login(name: $0) }
    @State private var selection: Login?

    var body: some View {
        NavigationView {
            List {
                ForEach(self.logins) { login in
                    VStack {
                        NavigationLink(destination: Text(login.name).font(.largeTitle),
                                       tag: login,
                                       selection: self.$selection) {
                            EmptyView()
                        }
                        Button(action: {
                            self.navigate(login: login)
                        }, label: {
                            Text(login.name)
                        })
                    }
                }
            }
        }
    }

    func navigate(login: Login) {
        guard let index = self.logins.firstIndex(of: login) else {
            fatalError()
        }

        self.logins[index].updateLastLogin() // remove this line
        self.selection = login
    }
}

推荐答案

这是工作方法的演示.使用 Xcode 12/iOS 14 测试

Here is a demo of working approach. Tested with Xcode 12 / iOS 14

struct DemoView: View {
    @State private var logins = ["Zaphod", "Arthur", "Ford", "Marvin", "Trillian"].map { Login(name: $0) }
    @State private var selection: UUID?

    var body: some View {
        NavigationView {
            List {
                ForEach(self.logins) { login in
                    VStack {
                        NavigationLink(destination: Text(login.name).font(.largeTitle),
                                       tag: login.id,
                                       selection: self.$selection) {
                            EmptyView()
                        }
                        Button(action: {
                            self.navigate(login: login)
                        }, label: {
                            Text(login.name)
                        })
                    }
                }
            }
        }
    }

    func navigate(login: Login) {
        guard let index = self.logins.firstIndex(of: login) else {
            fatalError()
        }

        self.logins[index].updateLastLogin() // remove this line
        self.selection = login.id
    }
}

这篇关于更新结构时 SwiftUI 导航不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆