有没有一种方法可以向NavigationLink添加额外的功能?SwiftUI [英] Is there a way to add an extra function to NavigationLink? SwiftUI
本文介绍了有没有一种方法可以向NavigationLink添加额外的功能?SwiftUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想向NavigationLink添加一个额外的功能.
I would like to add an extra function to the NavigationLink.
示例代码如下:
struct ContentView: View {
func yes () {
print("yes")
}
var body: some View {
NavigationView {
NavigationLink(destination: level1()) {
Text("Next")
}}}}
我知道这行不通,但是有可能这样做吗?(它将转到目的地并同时调用该函数)
I know this doesn't work, but is it possible to do something like this? (It will go to the destination and call the function at the same time)
NavigationLink(destination: level1(), yes()) {Text("Next")}
我尝试在NavigationLink内放置一个按钮,但是它也不起作用.当我这样做时,仅按钮中的功能有效,NavigationLink不起作用.
I tried putting a button inside the NavigationLink but it didn't work either. When I do this only the function in the button works, NavigationLink doesn't.
NavigationLink(destination: level1()) {
Button(action: { self.yes() })
{ Text("Button")}
}
推荐答案
使用onAppear(perform :) .这将在 View
的外观上执行某些功能.
Use the onAppear(perform:). This will perform some function on a View
's appear.
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: DetailView().onAppear {
self.someFunc()
}) {
Text("First Screen")
}
}
}
func someFunc() {
print("Click")
}
}
struct DetailView: View {
var body: some View {
Text("Second Screen")
}
}
这篇关于有没有一种方法可以向NavigationLink添加额外的功能?SwiftUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文