稍后如何在 SwiftUI 中访问内容视图的元素? [英] How to access content view's elements later in SwiftUI?

查看:18
本文介绍了稍后如何在 SwiftUI 中访问内容视图的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的内容视图:

Let's say that I have a content view like this one:

struct ContentView: View {
    @State private var selection = 0

    var body: some View {
        TabView(selection: $selection) {
            CustomClass()
                .tabItem {
                    VStack {
                        Image("First")
                        Text("First")
                    }
                }
                .tag(0)
            Button(action: { EmployeeStorage.sharedInstance.reload() }) {
                Text("Reload")
            }
                .tabItem {
                    VStack {
                        Image("Second")
                        Text("Second")
                    }
                }
                .tag(1)
        }
    }
}

// MARK: - SomeDelegateThatUpdatesMeLater
extension ContentView: SomeDelegateThatUpdatesMeLater {
    func callback() {
        // Here I want to update my content view's subviews
        // The ContentClass instance needs to be updated
    }
}

假设我想听一个回调方法,然后稍后更新内容视图的选项卡编号 1 (CustomClass).如何访问内容视图的子视图?我需要像 UIKit 的 subviewWithTag(_:) 这样的东西.SwiftUI 中是否有任何等价物?

Let's say that I want to listen to a callback method and then update the content view's tab number 1 (CustomClass) later on. How to access the content view's subviews? I'd need something like UIKit's subviewWithTag(_:). Is there any equivalent in SwiftUI?

推荐答案

您可能应该重新考虑您的方法.你到底想要发生什么?您有一些获取(或更新)数据的外部数据模型类型,并且您希望您的视图对此做出反应?如果是这种情况,请创建一个 ObservableObject 并将其传递给您的 CustomClass.

You should probably rethink your approach. What exactly is it you want to happen? You have some external data model type that fetches (or updates) data and you want your views to react to that? If that's the case, create an ObservableObject and pass that to your CustomClass.

struct CustomClass: View {

   @ObservedObject var model: Model

   var body: some View {
      // base your view on your observed-object
   }
}

也许您想收到来自 CustomClass 的事件的通知?

Perhaps you want to be notified of events that originate from CustomClass?

struct CustomClass: View {

  var onButtonPress: () -> Void = { }

  var body: some View {
    Button("Press me") { self.onButtonPress() }
  }
}

struct ParentView: View {

  var body: some View { 
    CustomClass(onButtonPress: { /* react to the press here */ })
  }
}

最后,如果你真的想在你的视图上添加某种标签,你可以利用SwiftUI中的Preferences系统.这是一个更复杂的主题,所以我将在这里指出我发现的一个很好的资源:https://swiftui-lab.com/communicating-with-the-view-tree-part-1/

Lastly, if you truly want some kind of tag on your views, you can leverage the Preferences system in SwiftUI. This is a more complicated topic so I will just point out what I have found to be a great resource here: https://swiftui-lab.com/communicating-with-the-view-tree-part-1/

这篇关于稍后如何在 SwiftUI 中访问内容视图的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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