Swiftui 如何使用NavigationDestinationLink转到其他视图?
我想使用NavigationDestinationLink转到另一个SwiftUI视图,但它不起作用 在这里,您可以看到我的按钮项代码,我想将其发送到我想要的页面,但它不起作用Swiftui 如何使用NavigationDestinationLink转到其他视图?,swiftui,Swiftui,我想使用NavigationDestinationLink转到另一个SwiftUI视图,但它不起作用 在这里,您可以看到我的按钮项代码,我想将其发送到我想要的页面,但它不起作用 var body: some View { Button(action: { if self.type == "origin"{ NavigationDestinationLink(SelectDestination(), isDetail: true)
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
仅在NavigationView
中起作用
因此,当您需要将导航移动到另一个视图时,只需使用包含文本的导航链接
或其他非交互式视图。导航链接
中定义的视图将用作按钮
:
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))
}
}
导入快捷界面
结构HomeButton:视图,其中内容:视图{
init(目标:内容,图标:字符串,标题:字符串,描述:字符串,颜色:颜色,宽度:长度=.0){
self.destination=目的地
self.icon=图标
self.title=标题
self.description=描述
self.color=颜色
self.width=宽度
}
让目标:内容
让图标:字符串
标题:字符串
let description:字符串
让颜色:颜色
让宽度:长度
var body:一些观点{
导航链接(目的地:目的地){
团体{
ZStack{
圈()
.foregroundColor(颜色)
.框架(宽60,高60)
文本(图标)
.font(.system(大小:55))
.偏移量(x:-25,y:20)
}.偏移量(x:12.5,y:0)
VStack{
正文(标题)
.fontWeight(.粗体)
.accentColor(.黑色)
正文(说明)
.accentColor(.黑色)
}.padding(.top,0)
}
.frame(宽度:宽度=.0?零:宽度)
.padding(.top,30)
.padding(.bottom,30)
.padding(.leading,45)
.padding(.training,45)
}
.背景(
颜色:白色
.转弯半径(6)
.阴影(颜色:浅灰色,半径:20))
}
}
那么它是做什么的呢?请包含您视图的完整代码以及所需的任何内容,以便人们可以实际测试您的代码。看见