试图在视图加载之后加载图像,驱动视图的模型对象(参见下面的MovieDetail)有一个urlString.因为SwiftUIView元素没有生命周期方法(并且没有视图控制器驱动),所以处理这个问题的最佳方式是什么?

我遇到的主要问题是,无论我try 使用哪种方法来解决问题(绑定对象或使用状态变量),我的视图在加载之后才会有urlString.

// movie object
struct Movie: Decodable, Identifiable {

    let id: String
    let title: String
    let year: String
    let type: String
    var posterUrl: String

    private enum CodingKeys: String, CodingKey {
        case id = "imdbID"
        case title = "Title"
        case year = "Year"
        case type = "Type"
        case posterUrl = "Poster"
    }
}
// root content list view that navigates to the detail view
struct ContentView : View {

    var movies: [Movie]

    var body: some View {
        NavigationView {
            List(movies) { movie in
                NavigationButton(destination: MovieDetail(movie: movie)) {
                    MovieRow(movie: movie)
                }
            }
            .navigationBarTitle(Text("Star Wars Movies"))
        }
    }
}
// detail view that needs to make the asynchronous call
struct MovieDetail : View {

    let movie: Movie
    @State var imageObject = BoundImageObject()

    var body: some View {
        HStack(alignment: .top) {
            VStack {
                Image(uiImage: imageObject.image)
                    .scaledToFit()

                Text(movie.title)
                    .font(.subheadline)
            }
        }
    }
}

提前谢谢您.

推荐答案

我希望这是有帮助的.我找到了a blogpost个关于在页面上做一些事情的导航视图.

idea 是将您的服务烘焙到一个BindableObject中,并在您的视图中订阅这些更新.

struct SearchView : View {
    @State private var query: String = "Swift"
    @EnvironmentObject var repoStore: ReposStore

    var body: some View {
        NavigationView {
            List {
                TextField($query, placeholder: Text("type something..."), onCommit: fetch)
                ForEach(repoStore.repos) { repo in
                    RepoRow(repo: repo)
                }
            }.navigationBarTitle(Text("Search"))
        }.onAppear(perform: fetch)
    }

    private func fetch() {
        repoStore.fetch(matching: query)
    }
}
import SwiftUI
import Combine

class ReposStore: BindableObject {
    var repos: [Repo] = [] {
        didSet {
            didChange.send(self)
        }
    }

    var didChange = PassthroughSubject<ReposStore, Never>()

    let service: GithubService
    init(service: GithubService) {
        self.service = service
    }

    func fetch(matching query: String) {
        service.search(matching: query) { [weak self] result in
            DispatchQueue.main.async {
                switch result {
                case .success(let repos): self?.repos = repos
                case .failure: self?.repos = []
                }
            }
        }
    }
}

归功于:马吉德·贾布拉伊洛夫

Ios相关问答推荐

实例方法wait在异步上下文中不可用;请改用TaskGroup;这是Swift 6中的错误''

在滚动视图中固定页眉,但在页眉上方嵌入元素

如何使用参与者允许并行读取,但阻止对SWIFT中资源的并发读取和写入?

使用标头UICollectionViewFlowLayout时的简单SwiftUI视图错误

如何删除NavigationView中的默认透明标头?

Xcode 15模拟器用x86_64编译

在 Android 和 iOS 应用程序上下文中阻止后台线程有哪些缺点?

SwiftUI HStack 文本字段对齐

SKScene 的更新功能似乎不起作用

使用 SwiftUI 显示多个 VNRecognizedObjectObservation 边界框时偏移量错误

xcode-select:找不到clang

List touch Tabbar 时 SwiftUI Tabbar 消失

如何在 Swift 中从另一个视图控制器将元素附加到数组中

在 tableview 中下载图像缩略图时如何提高滚动性能

UIButton在iOS7中没有显示突出显示

Xcode 缺少支持文件 iOS 12.2 (16E227)

在 Grand Central Dispatch 中使用 dispatch_sync

如何使用 sendAsynchronousRequest:queue:completionHandler:

如何以编程方式切换 UISegmentedControl?

Info.plist 实用程序错误:无法打开 Info.plist,因为没有这样的文件