【问题标题】:Call a function when a button is clicked in SwiftUI在 SwiftUI 中单击按钮时调用函数
【发布时间】:2021-10-08 09:32:43
【问题描述】:

我已经从开发应用程序从 Obj-C 切换到 swiftUI。 我有一个按钮,当点击它应该显示弹出视图。按照此链接显示弹出窗口https://blog.techchee.com/how-to-create-a-pop-up-view-with-swiftui/

但单击按钮时不会出现弹出窗口。这是我的代码

struct Location: View {
var body: some View {
   ZStack{
           Button(action: {
                popUpView()
           

        }, label: {
            Text("Select Location")
                .frame(minWidth: 0, maxWidth: 500)
                .padding()
                .background(Color.clear)
                .foregroundColor(Color.black)
                .font(.custom("Open Sans", size: 18))
                .overlay(
                          RoundedRectangle(cornerRadius: 10)
                           .stroke(Color.gray, lineWidth: 2)
                                )
        })
       



}
}

private let choices = ["Apple", "Orange", "Papaya", "Grape", "Star Fruit", "Strawberries", "Raspberries", "Blueberries"].sorted()

   
    func popUpView()-> some View  {
        
      
        VStack (spacing : 10) {
              Text("Choices Of Fruits").font(Font.custom("Avenir-Black", size: 18.0))
                .position(x:100 , y:400)
                  List(choices, id:\.self) { Text($0) }
                  Button(action: {
                      withAnimation {

                       }
                   }, label: {
                           Text("Close")
                   })
           
           }
               .padding()
               .frame(width: 240, height: 300)
               .background(Color.white)
               .cornerRadius(20)
               .shadow(radius: 20 )

    }
    
}

请帮忙看看哪里出错了?

【问题讨论】:

    标签: ios swift ipad swiftui


    【解决方案1】:

    根据您的设计,您可以在主 ZStack 中添加条件并隐藏显示视图。 此外,您可以使用工作表。

    struct Location: View {
        @State private var isPresent: Bool = false // <-- Here
        var body: some View {
            ZStack{
                Button(action: {
                    withAnimation {
                        isPresent = true // <-- Here
                    }
                    
                }, label: {
                    Text("Select Location")
                        .frame(minWidth: 0, maxWidth: 500)
                        .padding()
                        .background(Color.clear)
                        .foregroundColor(Color.black)
                        .font(.custom("Open Sans", size: 18))
                        .overlay(
                            RoundedRectangle(cornerRadius: 10)
                                .stroke(Color.gray, lineWidth: 2)
                        )
                })
                
                if isPresent { // <-- Here
                    popUpView()
                }
            }
        }
        
        private let choices = ["Apple", "Orange", "Papaya", "Grape", "Star Fruit", "Strawberries", "Raspberries", "Blueberries"].sorted()
        
        
        func popUpView()-> some View  {
            
            
            VStack (spacing : 10) {
                Text("Choices Of Fruits").font(Font.custom("Avenir-Black", size: 18.0))
                    .position(x:100 , y:400)
                List(choices, id:\.self) { Text($0) }
                Button(action: {
                    withAnimation {
                        isPresent = false // <-- Here
                    }
                }, label: {
                    Text("Close")
                })
                
            }
            .padding()
            .frame(width: 240, height: 300)
            .background(Color.white)
            .cornerRadius(20)
            .shadow(radius: 20 )
            
        }
        
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 2022-11-14
      相关资源
      最近更新 更多