SwiftUI:强制更新 [英] SwiftUI: Forcing an Update

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

问题描述

通常情况下,我们被限制讨论 Apple 预发布的内容,但我已经看到很多 SwiftUI 的讨论,所以我怀疑这没关系;就这一次.

Normally, we're restricted from discussing Apple prerelease stuff, but I've already seen plenty of SwiftUI discussions, so I suspect that it's OK; just this once.

我正在学习其中一个教程(我这样做).

I am in the process of driving into the weeds on one of the tutorials (I do that).

我在与 UIKit 交互"教程中的可滑动屏幕下方添加了一对按钮:https://developer.apple.com/tutorials/swiftui/interface-with-uikit

I am adding a pair of buttons below the swipeable screens in the "Interfacing With UIKit" tutorial: https://developer.apple.com/tutorials/swiftui/interfacing-with-uikit

这些是下一个"和上一个"按钮.当在一端或另一端时,相应的按钮会隐藏.我的工作正常.

These are "Next" and "Prev" buttons. When at one end or the other, the corresponding button hides. I have that working fine.

我遇到的问题是访问由 PageViewController 表示的 UIPageViewController 实例.

The problem that I'm having, is accessing the UIPageViewController instance represented by the PageViewController.

我更改了 currentPage 属性(通过使 PageViewController 成为 UIPageViewController 的委托),但我需要强制 UIPageViewController 以编程方式更改.

I have the currentPage property changing (by making the PageViewController a delegate of the UIPageViewController), but I need to force the UIPageViewController to change programmatically.

我知道我可以通过重绘 PageView 主体来蛮力"显示,反映一个新的 currentPage,但我不确定如何做到这一点.

I know that I can "brute force" the display by redrawing the PageView body, reflecting a new currentPage, but I'm not exactly sure how to do that.

struct PageView<Page: View>: View {
    var viewControllers: [UIHostingController<Page>]
    @State var currentPage = 0

    init(_ views: [Page]) {
        self.viewControllers = views.map { UIHostingController(rootView: $0) }
    }

    var body: some View {
        VStack {
            PageViewController(controllers: viewControllers, currentPage: $currentPage)

            HStack(alignment: .center) {
                Spacer()

                if 0 < currentPage {
                    Button(action: {
                        self.prevPage()
                    }) {
                        Text("Prev")
                    }

                    Spacer()
                }

                Text(verbatim: "Page \(currentPage)")

                if currentPage < viewControllers.count - 1 {
                    Spacer()

                    Button(action: {
                        self.nextPage()
                    }) {
                        Text("Next")
                    }
                }

                Spacer()
            }
        }
    }

    func nextPage() {
        if currentPage < viewControllers.count - 1 {
            currentPage += 1
        }
    }

    func prevPage() {
        if 0 < currentPage {
            currentPage -= 1
        }
    }
}

我知道答案应该是显而易见的,但我很难弄清楚如何以编程方式刷新 VStack 或正文.

I know the answer should be obvious, but I'm having difficulty figuring out how to programmatically refresh the VStack or body.

推荐答案

设置currentPage,因为它是一个@State,会重新加载整个body.

Setting currentPage, as it is a @State, will reload the whole body.

这篇关于SwiftUI:强制更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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