SwiftUI NavigationLink loads destination view immediately, without clicking

前端 未结 4 1760
不知归路
不知归路 2020-12-05 13:01

With following code:

struct HomeView: View {
    var body: some View {
        NavigationView {
            List(dataTypes) { dataType in
                Na         


        
4条回答
  •  北海茫月
    2020-12-05 13:35

    I had the same issue where I might have had a list of 50 items, that then loaded 50 views for the detail view that called an API (which resulted in 50 additional images being downloaded).

    The answer for me was to use .onAppear to trigger all logic that needs to be executed when the view appears on screen (like setting off your timers).

    struct AnotherView: View {
        var body: some View {
            VStack{
                Text("Hello World!")
            }.onAppear {
                print("I only printed when the view appeared")
                // trigger whatever you need to here instead of on init
            }
        }
    }
    

提交回复
热议问题