4

iOS 15 引入了“.refreshable”视图修饰符,但我见过的大多数示例都使用List. 我想在一个不使用列表而只VStack使用Text如下视图的应用程序中实现拉动刷新功能。我该如何实现这一点,以便下拉刷新将刷新我的数据?

import SwiftUI

struct ContentView: View {

@State var songName = "Song"
@State var artistName = "Artist"

    var body: some View {
        VStack {
            Text(songName)
            Text(artistName)
        }
        .refreshable {
            reloadData()
        }
    }
    
    private func reloadData() {
        
        songName = "Let it Be"
        artistName = "The Beatles"
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
4

0 回答 0