SwiftUI 如何通过Button控制NavigationView的切换
想做个小实验,希望实现通过button来控制视图的切换。Google好半天,才找到一个解决方案,可以运行,但是感觉代码不够优雅。
import SwiftUI
struct ContentView: View {
@State var selection: Int? = nil
var body: some View {
NavigationView{
List{
NavigationLink(destination: TextDemoView(), tag: 1, selection: $selection) {
Button(action:{
print("hello text")
self.selection = 1
}){
Text(" Text 文本框 ")
.foregroundColor(.white)
.background(Color.purple)
.cornerRadius(10)
.shadow(radius: 4)
}
}
}
.navigationBarTitle(Text("基础控件"))
}
}
}
参考代码
更多SwiftUI教程和代码关注专栏
- 请关注我的专栏 SwiftUI教程与源码