【问题标题】:Can't sheet with button in ListView SwiftUI无法在 ListView SwiftUI 中使用按钮工作表
【发布时间】:2021-08-20 08:20:04
【问题描述】:

我有一个清单。在列表中有 2 个按钮。 我想单击每个按钮以使用 Sheet 呈现另一个视图。 当我第一次单击它时它可以工作,但是第二次或另一个点击按钮它不显示视图。希望你能帮助我。

My code

My design

【问题讨论】:

标签: swiftui swiftui-list


【解决方案1】:

阅读如何使用按钮和表格。触发工作表的按钮通常是这样使用的:

编辑:根据 Philip Dukhov 的建议,替换:

        Button("Learning") {
           
                }.sheet(isPresented: $isLearning, content: {
                    LearningView()
                })
        .onTapGesture {
            self.isLearning = true
        }

与:

    Button(action: {isLearning = true}) {
        Text("Learning")
    }
    .sheet(isPresented: $isLearning) {
        LearningView()
    }

并对“Test”按钮执行相同操作,使用“isTest”代替“isLearning”,使用“TestViewCategory()”代替“LearningView()”。

编辑 2:

更新“TestView”:

struct TestView: View {
    var title = ""
    let models = modelItems
    
    var body: some View {
        ScrollView {
            VStack {
                ForEach(models) { model in
                    TopicList(model: model)
                }
            }
        }
        .navigationBarTitle(title, displayMode: .inline)
        .onAppear {
            UITableView.appearance().separatorStyle = .none
        }
        .animation(.spring())
    }
}

编辑 3:这是适合我的测试代码:

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            TestView()
        }
    }
}

struct ButtonView: View {
    
    @State var isLearning: Bool = false
    @State var isTest: Bool = false
    
    var body: some View {
        HStack {
            Button(action: {isLearning = true}) {
                Text("Learning")
            }
            .sheet(isPresented: $isLearning) {
                Text("LearningView")
            }
            .font(.system(size: 16))
            .frame(width: 132 , height: 48)
            .background(Color(.white))
            .overlay(
                RoundedRectangle(cornerRadius: 30)
                    .stroke(Color(#colorLiteral(red: 0.9259920716, green: 0.9261471629, blue: 0.9259716272, alpha: 1)), lineWidth: 1)
            )
            Button(action: {isTest = true}) {
                Text("Test")
            }
            .sheet(isPresented: $isTest) {
                Text("TestViewCategory")
            }
            .font(.system(size: 16))
            .frame(width: 132 , height: 48)
            .background(Color(.white))
            .overlay(
                RoundedRectangle(cornerRadius: 30)
                    .stroke(Color(#colorLiteral(red: 0.9259920716, green: 0.9261471629, blue: 0.9259716272, alpha: 1)), lineWidth: 1)
            )
        }
    }
}

struct TopicList: View {
    // for testing
    let model: String
    
    @State private var showSubItem = false
    
    var body: some View {
        VStack {
            HStack {
                Image(systemName: showSubItem ? "arrow.up.circle" : "arrow.down.circle")
                    .resizable()
                    .frame(width: 26, height: 26)
                    .onTapGesture {
                        withAnimation {
                            showSubItem.toggle()
                        }
                    }
                
                VStack {
                    VStack(alignment: .leading) {
                        Text("title")
                            .font(.custom("Poppins-Regular", size: 24))
                            .padding(.top,9)
                            .padding(.bottom,4)
                        
                        HStack {
                            Text("Open date")
                                .font(.custom("Poppins-Regular", size: 12))
                            Text("Open date")
                                .font(.custom("Poppins-Regular", size: 12))
                            Text("Due date")
                                .font(.custom("Poppins-Regular", size: 12))
                            Text("Due date")
                                .font(.custom("Poppins-Regular", size: 12))
                        }
                    }
                    .padding(.leading,17)
                    .frame(width: 320, height: 70)
                    .fixedSize(horizontal: false, vertical: true)
                    
                    if showSubItem {
                        ButtonView()
                        .padding(.top,12)
                        .fixedSize(horizontal: false, vertical: true)
                        .transition(.opacity)
                        .transition(.slide)
                        .padding(.bottom,13)
                    }
                }
                .overlay(
                    RoundedRectangle(cornerRadius: 10)
                        .stroke(Color(#colorLiteral(red: 0.9259920716, green: 0.9261471629, blue: 0.9259716272, alpha: 1)), lineWidth: 1)
                )
            }
        }
    }
}

struct TestView: View {
    var title = "nav title"
    let models = ["1","2","3"]
    
    var body: some View {
        ScrollView {
            VStack {
                ForEach(models, id: \.self) { model in
                    TopicList(model: model)
                }
            }
        }
        .navigationBarTitle(title, displayMode: .inline)
        .onAppear {
            UITableView.appearance().separatorStyle = .none
        }
        .animation(.spring())
    }
}

【讨论】:

  • 还是不行。 isLearning = 真。只在onTapGesture里面有效,但是当我改变它时,它仍然不起作用
  • 去掉onTapGesture,给我们看你正在使用的代码,而不是图片。
  • 我已经更新了我的答案,你可以试试。
  • 您不需要手动设置isLearning = false,因为您将绑定传递给isPresented,并且在关闭时它将设置为false
  • 我跟着你了,但还是不行:((
猜你喜欢
  • 1970-01-01
  • 2020-05-30
  • 2020-12-29
  • 2018-07-24
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 2023-01-11
相关资源
最近更新 更多