Swiftui 点击NavigationLink后如何执行操作?
我在第一个视图中有一个加号按钮。看起来像一个晶圆厂按钮。我想在点击NavigationLink中包装的某个步骤后隐藏它。到目前为止,我有这样的想法:Swiftui 点击NavigationLink后如何执行操作?,swiftui,Swiftui,我在第一个视图中有一个加号按钮。看起来像一个晶圆厂按钮。我想在点击NavigationLink中包装的某个步骤后隐藏它。到目前为止,我有这样的想法: ForEach(0 ..< 12) {item in NavigationLink(destination: TransactionsDetailsView()) { VStack { HStack(alignment: .top) { Text("List it
ForEach(0 ..< 12) {item in
NavigationLink(destination: TransactionsDetailsView()) {
VStack {
HStack(alignment: .top) {
Text("List item")
}
.padding(EdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10))
.foregroundColor(.black)
Divider()
}
}
.simultaneousGesture(TapGesture().onEnded{
self.showPlusButton = false
})
.onAppear(){
self.showPlusButton = true
}
}
ForEach(0..<12){item in
导航链接(目标:TransactionDetailsView()){
VStack{
HStack(对齐:。顶部){
文本(“列表项”)
}
.填充(边组(顶部:5个,前导:10个,底部:5个,尾随:10个))
.foregroundColor(.黑色)
分隔器()
}
}
.同时手势(轻触手势().ONENED{
self.showPlusButton=false
})
.onAppear(){
self.showPlusButton=true
}
}
它的工作原理与单水龙头。但当我长按NavigationLink时,它不起作用。我应该如何重写我的代码以包括长按?或者我应该让它与使用同时手势不同?是的,
NavigationLink
不允许这种同时手势(可能是设计的,可能是由于问题,等等)
您期望的行为可能实现如下(当然,如果列表项中需要一些V形,则需要手动添加)
导入快捷界面
结构测试同时手势:视图{
@状态变量showPlusButton=false
@状态变量currentTag:Int?
var body:一些观点{
导航视图{
名单{
ForEach(0..<12){
VStack{
HStack(对齐:。顶部){
文本(“列表项”)
导航链接(目的地:文本(“详细信息”),标记:项目,选择:self.$currentTag){
EmptyView()
}
}
.填充(边组(顶部:5个,前导:10个,底部:5个,尾随:10个))
.foregroundColor(.黑色)
分隔器()
}
.同时手势(轻触手势().ONENED{
打印(“点击”)
self.currentTag=项目
self.showPlusButton=false
})
.Simultaneous手势(LongPress手势().onEnded{uu}in
打印(“长按”)
self.currentTag=项目
self.showPlusButton=false
})
.onAppear(){
self.showPlusButton=true
}
}
}
}
}
}
结构测试同时手势预览:预览提供程序{
静态var预览:一些视图{
testsimultaneousposition()
}
}
我正在使用以下代码。我更喜欢它本身就是NavigationLink
,因为它允许我重用现有的按钮样式
结构导航按钮:视图{
变量操作:()->Void={}
变量目的地:()->目的地
变量标签:()->标签
@国家私有变量isActive:Bool=false
var body:一些观点{
按钮(操作:{
自我行动
self.isActive.toggle()
}) {
self.label()
.背景(
ScrollView{//修复了导航栏可能隐藏在推送视图上的错误
NavigationLink(目的地:LazyDestination{self.destination()},
isActive:self.$isActive){EmptyView()}
}
)
}
}
}
//此视图使我们避免在推送目标之前实例化目标。
结构LazyDestination:视图{
变量目的地:()->目的地
var body:一些观点{
self.destination()
}
}
使用它:
var主体:一些视图{
导航按钮(
操作:{打印(“点击!”)},
目标:{Text(“推式视图”)},
标签:{Text(“点击我”)}
)
}
我尝试了另一种解决问题的方法。最初我没有使用“列表”,因为我的部分代码有问题。但这会导致另一个问题:点击NavigationLink后,PlusButton不会在下一个屏幕上消失。这就是为什么我想同时使用手势——点击链接后,一些动作也会被执行(这里:PlusButton会被隐藏)。但效果并不好
我尝试了另一种解决办法。使用列表(也许我以后会解决另一个问题)
这是我的替代代码。完全不需要同时手势。V形符号会自动添加到列表中。而PlusButton会隐藏我想要的
import SwiftUI
struct BookingView: View {
@State private var show_modal: Bool = false
var body: some View {
NavigationView {
ZStack {
List {
DateView()
.listRowInsets(EdgeInsets())
ForEach(0 ..< 12) {item in
NavigationLink(destination: BookingDetailsView()) {
HStack {
Text("Booking list item")
Spacer()
}
.padding()
}
}
}.navigationBarTitle(Text("Booking"))
VStack {
Spacer()
Button(action: {
print("Button Pushed")
self.show_modal = true
}) {
Image(systemName: "plus")
.font(.largeTitle)
.frame(width: 60, height: 60)
.foregroundColor(Color.white)
}.sheet(isPresented: self.$show_modal) {
BookingAddView()
}
.background(Color.blue)
.cornerRadius(30)
.padding()
.shadow(color: Color.black.opacity(0.3), radius: 3, x: 3, y: 3)
}
}
}
}
}
导入快捷界面
结构BookingView:视图{
@国家私有变量显示模式:Bool=false
var body:一些观点{
导航视图{
ZStack{
名单{
日期视图()
.listRowInsets(EdgeInsets())
ForEach(0..<12){
导航链接(目标:BookingDetailsView()){
HStack{
文本(“预订列表项目”)
垫片()
}
.padding()
}
}
}.navigationBarTitle(文本(“预订”))
VStack{
垫片()
按钮(操作:{
打印(“按下按钮”)
self.show_modal=true
}) {
图像(系统名称:“plus”)
.font(.largeTitle)
.框架(宽60,高60)
.foregroundColor(颜色.白色)
}.sheet(显示:self.$show\u模态){
BookingAddView()
import SwiftUI
struct BookingView: View {
@State private var show_modal: Bool = false
var body: some View {
NavigationView {
ZStack {
List {
DateView()
.listRowInsets(EdgeInsets())
ForEach(0 ..< 12) {item in
NavigationLink(destination: BookingDetailsView()) {
HStack {
Text("Booking list item")
Spacer()
}
.padding()
}
}
}.navigationBarTitle(Text("Booking"))
VStack {
Spacer()
Button(action: {
print("Button Pushed")
self.show_modal = true
}) {
Image(systemName: "plus")
.font(.largeTitle)
.frame(width: 60, height: 60)
.foregroundColor(Color.white)
}.sheet(isPresented: self.$show_modal) {
BookingAddView()
}
.background(Color.blue)
.cornerRadius(30)
.padding()
.shadow(color: Color.black.opacity(0.3), radius: 3, x: 3, y: 3)
}
}
}
}
}
ForEach(0 ..< 12) {item in
NavigationLink(destination: TransactionsDetailsView()) {
VStack {
HStack(alignment: .top) {
Text("List item")
}
.padding(EdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10))
.foregroundColor(.black)
Divider()
}
}
.onDisappear(){
self.showPlusButton = false
}
.onAppear(){
self.showPlusButton = true
}
}