Swiftui中的每一个按钮的每次点击都有不同的动作

发布于 2025-01-24 03:13:31 字数 1566 浏览 2 评论 0原文

我正在使用显示/hide 在Swiftui中的视图。当我第一次点击按钮时,它可以正常工作。像这样:

“我的观点”

我想要的是,当我第一次点击时,它必须向我展示我的观点。当我点击时,它是 second 时间时,它必须再次隐藏视图。在上,每次点击必须分别显示/hide 视图。在故事板中,此方案很容易处理,但是在 swiftui 中,我不知道该如何做到这一点,因为我是Swiftui的新手。

我的代码是:


import SwiftUI

struct ShowHideText: View {
    
    @State private var showName = false
    
    var body: some View {
        VStack(spacing: 50) {
            
            Text("Users")
                .fontWeight(.bold)
            
            if showName {
                name()
            } else {
                name()
                    .hidden()
            }
            
            
            Button("Show/Hide") {
                showName = true
            }
        }
    }
    
    func name() -> some View {
        
        VStack(spacing: 30) {
            Text("David")
            
            Text("Kate")
        }
        
    }
}

struct ShowHideText_Previews: PreviewProvider {
    static var previews: some View {
        ShowHideText()
    }
}

我需要问的另一件事是在我的代码中,如果我使用 .hidded(),则视图 hides ,但是视图的空间仍然存在那里。我不知道它是否有一种隐藏视图的方法,而且我也有视图的内容,因为我也必须在其内容上做一些工作,或者如果我不使用 .hidden() name() in else part,我可以对其内容进行一些工作。

I am using a button to show/hide a view in SwiftUI. It is working fine when I first tap the button. Like this:

my view

The thing I want is when I first tap it, it has to show me my view. And when I tap it the second time, it has to again hide the view. And on every tap it has to show/hide the view respectively. In storyboard, this scenario is easy to handle but in SwiftUI, I don't know how to do this as I am new to SwiftUI.

My code is:


import SwiftUI

struct ShowHideText: View {
    
    @State private var showName = false
    
    var body: some View {
        VStack(spacing: 50) {
            
            Text("Users")
                .fontWeight(.bold)
            
            if showName {
                name()
            } else {
                name()
                    .hidden()
            }
            
            
            Button("Show/Hide") {
                showName = true
            }
        }
    }
    
    func name() -> some View {
        
        VStack(spacing: 30) {
            Text("David")
            
            Text("Kate")
        }
        
    }
}

struct ShowHideText_Previews: PreviewProvider {
    static var previews: some View {
        ShowHideText()
    }
}

And one more thing which I need to ask is in my code, if I use .hidden(), the view hides but the space of the view remains there. I don't know if there is a way that it hides the view and I also have the view's content as I have to do some work on its content too, or if it works fine if I don't use .hidden() and name() in else part and I can do some work with its content.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情空心 2025-01-31 03:13:31

更改

Button("Show/Hide") {
   showName = true
}    

Button("Show/Hide") {
   showName.toggle()
}

Change

Button("Show/Hide") {
   showName = true
}    

to

Button("Show/Hide") {
   showName.toggle()
}
俏︾媚 2025-01-31 03:13:31

对于您的问题的第二部分。更改

if showName {
    name()
} else {
    name()
        .hidden()
}

if showName {
    name()
}

For second part of your question. Change

if showName {
    name()
} else {
    name()
        .hidden()
}

To

if showName {
    name()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文