Swift UI @State 确实在第一次点击时设置 [英] Swift UI @State does get set on the first tap

查看:18
本文介绍了Swift UI @State 确实在第一次点击时设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的自定义视图列表.需要发生的是选择其中一个项目时,它需要显示该项目的详细视图.在我的代码中,当我第一次选择一个项目时,@Statet selectedListener 没有设置,因此不会出现详细信息视图.如果我继续选择以前选择的相同项目,则行为保持不变.但是,如果我选择一个不同的项目,它可以正常工作,我可以回到上一个项目并查看其详细信息视图而不会出现问题.这是我正在尝试做的一件简单的事情,只是无法弄清楚我可能做错了什么.任何帮助,将不胜感激.谢谢.

I have a simple list of for a Custom View. What needs to happen is when one of the items are selected it needs to show the detail view for that item. In my code, when I select an item for the very first time, the @Statet selectedListener does not get set so the detail view does not appear. If I keep selecting the same item that was previously selected the behaviour remains the same. However, if I select a different item, it works properly and I can come back to the previous item and view its detail view without an issue. Its such a simple thing I'm trying to do and just can't figure out what I'm may be doing wrong. Any help would be appreciated. Thank You.

struct ListenersListView<Model>: View where Model: ActiveListenerViewModel {

@ObservedObject var viewModel: Model
@State var showDetail = false
@State var selectedListener: UserProfile? = nil

var body: some View {
    ScrollView {
        VStack(spacing:20) {
            ForEach(viewModel.allActiveListeners) { listener in
                UserImageSection(listener: listener,
                                 subtext: listener.supportAreas)
                    .onTapGesture(perform: {
                        handleTap(listener)
                    })
            }
        }
        
    }
    .padding(.horizontal,20)
    .sheet(isPresented: $showDetail, content: {
        if let listener = selectedListener {
            ListenerDetailView(listener: listener)
        }
        else {
            // It should never come here but when the first item is selected it does. The only way to not come here is to select another item after the first item has been selected.
            Text("Listener not loaded yet")
        }
    })
    
    
}

private func handleTap(_ listener: UserProfile) {
    print("Showing listener \(listener.id)")
    self.selectedListener = listener
    showDetail = true
    
}

}

推荐答案

在 iOS 14 中,为了让 .sheet 呈现最新数据,您必须使用 sheet(item:) 而不是 .sheet(isPresented:)

In iOS 14, in order to get the .sheet to be presented with up-to-date data, you must use sheet(item:) instead of .sheet(isPresented:)

你可以在这里看到我的另一个答案:https://stackoverflow.com/a/66190152/560942SwiftUI:在枚举上切换 .sheet,不起作用

You can see another one of my answers here: https://stackoverflow.com/a/66190152/560942 and SwiftUI: Switch .sheet on enum, does not work

在您的具体情况下,很难说,因为我没有您的 UserProfile 代码,但我建议您:

In your specific case, it's hard to tell since I don't have your UserProfile code, but I suggest that you:

  1. 摆脱你的 showDetail 状态变量
  2. 确保UserDetail符合Identifiable
  3. 将您的 sheet(isPresented:) 更改为:
  1. Get rid of your showDetail state variable
  2. Make sure that UserDetail conforms to Identifiable
  3. Change your sheet(isPresented:) to:

.sheet(item: $selectedListener) { listener in 
  ListenerDetailView(listener: listener)
} 

这篇关于Swift UI @State 确实在第一次点击时设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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