swiftui - 如何使用 NavigationDestinationLink 转到另一个视图?
问题描述
我想使用 NavigationDestinationLink 转到另一个 SwiftUI 视图,但它不起作用。
在这里你可以看到我想要发送到我想要的页面但它不起作用的按钮项的代码
var body: some View {
Button(action: {
if self.type == "origin"{
NavigationDestinationLink(SelectDestination(), isDetail: true)
} else if self.type == "destination"{
NavigationDestinationLink(TravelCostPage())
}
print("Button Clicked")
}) {
HStack{
Spacer()
VStack {
Text(title)
.bold()
.color(.black)
Text("توضیحات")
.font(.footnote)
.color(.gray)
}
Image("grayStarIcon")
.renderingMode(.original)
.frame(width: 30, height: 30)
}.padding()
.frame(width: UIScreen.main.bounds.width, height: 50)
}
}
解决方案
这样做的正确对象是NavigationLink
. 有了NavigationLink
你可以控制导航。重要的是要注意NavigationLink
仅在NavigationView
.
因此,当您需要将导航移动到另一个视图时,您必须只使用NavigationLink
包含一个Text
或其他一些非交互式视图。内部定义的视图NavigationLink
将用作Button
:
NavigationLink(destination: DestinationView()) {
Text("click me")
}
高级示例:
import SwiftUI
struct HomeButton<Content>: View where Content: View {
init(destination: Content, icon: String, title: String, description: String, color: Color, width: Length = .zero) {
self.destination = destination
self.icon = icon
self.title = title
self.description = description
self.color = color
self.width = width
}
let destination: Content
let icon: String
let title: String
let description: String
let color: Color
let width: Length
var body: some View {
NavigationLink(destination: destination) {
Group {
ZStack {
Circle()
.foregroundColor(color)
.frame(width: 60, height: 60)
Text(icon)
.font(.system(size: 55))
.offset(x: -25, y: 20)
}.offset(x: 12.5, y: 0)
VStack {
Text(title)
.fontWeight(.bold)
.accentColor(.black)
Text(description)
.accentColor(.black)
}.padding(.top, 0)
}
.frame(width: width == .zero ? nil : width)
.padding(.top, 30)
.padding(.bottom, 30)
.padding(.leading, 45)
.padding(.trailing, 45)
}
.background(
Color.white
.cornerRadius(6)
.shadow(color: Color.lightGray, radius: 20))
}
}
推荐阅读
- python - 无法在 python 中的 virtualenv 上安装“gpustat”
- azure-devops - 我们如何在 Azure Devops 中将大量工作项从一个项目复制到另一个项目?
- amazon-web-services - AWS S3/CloudFront 忽略内容处置/类型
- wcf - 在 .net 核心中使用 wcf 服务来使用 services.AddSingleton 或 services.AddTransient
- java - 前往铭文页面
- javascript - 从 Express Router 中分离 Mongoose 代码
- javascript - axios javascript中的文件下载损坏
- devops - Apple Pay 显示域验证失败。无法访问服务器上的验证文件
- excel - 文件夹选择器问题 - VBA
- react-native - React Native Navigation - 无效的挂钩调用