Swift/SwiftUI 将不同的函数传递到视图中

huangapple go评论58阅读模式
英文:

Swift/SwiftUI Pass different functions into a view

问题

我尝试将不同的函数传递到自定义样式的按钮和NavigationLink中

```Swift
struct commonButtonClass: View {
    let buttonText: String
    
    var body: some View {
        Button(action: {
            // 在每个实例中使用不同的函数。例如,可以传递登录或注册函数
            }
        }, label: {
            Text(buttonText)
                .font(.system(size:30))
        })
    }
}

然而,我无法找到一种将函数传递到View中的方法。我尝试了使用CGFunction分配给一个变量,然后在调用此View时传递login()或register(),但它不起作用。

我尝试了类似的方法,用于自定义样式的NavigationLink的目标,但是将View或String分配为目标不允许我将其设置为下一个目标,例如LoginView()或LandingView()。

struct commonNavigationLinkClass: View {
    let target: String
    // 所需的目标
    let linkText: String
    
    var body: some View {
        NavigationLink(destination: View(target), label: {
            Text(linkText)
                .font(.system(size:30))
        })
    }
}

对于按钮,我尝试使用一个CGFunction类来将函数传递到按钮中,但它给出了类和内容不兼容的错误。

对于NavigationLink,我尝试使用一个包含View的字符串(View(//此处为字符串)),但它不起作用。将目标设置为任何View类也不起作用。

也许有更好的方法来处理这个问题,而不是将内容传递下去,但是我不太确定如何以其他方式实现所有这些类型的按钮和NavigationLink的样式。除了字体之外,还有其他样式,但出于简单起见,我删除了它。


<details>
<summary>英文:</summary>

I am trying to pass in different functions into a custom styling View for a button and a NavigationLink.
```Swift
struct commonButtonClass: View {
    let buttonText: String
    
    var body: some View {
        Button(action: {
            // a different function in each instance. it can be for example the login or register function passed in
            }
        }, label: {
            Text(buttonText)
                .font(.system(size:30))
        })
    }
}

However I can't find a way to pass through a function into the View. I was trying the CGFunction assignment for a variable and then passing in login() or register() in the call for this View but it does not work.

I tried a similar approach with the destination for the custom styling of NavigationLink but assigning View nor String allowed me to set it as the next destination like LoginView() or LandingView().

struct commonNavigationLinkClass: View {
    let target: String
// the desired destination 
    let linkText: String
    
    var body: some View {
        NavigationLink(destination: View(target), label: {
            Text(linkText)
                .font(.system(size:30))
        })
    }
}

For the button, I tried using a CGFunction class for passing the function into the button but it gave me errors that the class and contents are not compatible.

For the NavigationLink, I tried using a String with a View(//string here) but it did not work. Nor did setting the target as a class of any View.

Maybe there is a better way to go about this without passing the contents down but I'm not quite sure how to achieve the styling for all these types of button and NavigationLink otherwise. there's other styling than just font but it's just colour and borders so I removed it for simplicity sake

答案1

得分: 0

你需要将闭包作为参数传递...

struct CommonButtonView: View {

    let buttonText: String
    let action: () -> Void
    
    var body: some View {
        Button(action: action, label: {
            Text(buttonText)
                .font(.system(size: 30))
        })
    }
}

然后像这样使用:

struct ContentView: View {

    var body: some View {
        CommonButtonView(buttonText: "按下我") {
            // 一些操作
        }
    }
}

但最好直接使用普通的 Button 并定义自己的自定义 ButtonStyle

英文:

You need to take a closure as a parameter…

struct CommonButtonView: View {

    let buttonText: String
    let action: () -&gt; Void
    
    var body: some View {
        Button(action: action, label: {
            Text(buttonText)
                .font(.system(size:30))
        })
    }
}

Then use like:

struct ContentView: View {

    var body: some View {
        CommonButtonView(buttonText: &quot;Press me&quot;) {
            // some action 
        }
    }
}

But you're probably better off just using the regular Button and defining your own custom ButtonStyle

huangapple
  • 本文由 发表于 2023年2月14日 21:34:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定