SwiftUI - 使用 NavigationView 切换视图
SwiftUI - Switching views with NavigationView
我正在尝试向我的应用添加评论。为此,我显然需要切换我的应用程序的视图。目前,我有两个文件:PostView 和 PostRow。 PostView 是实际的视图文件,PostRow 是显示帖子的文件。
我正在尝试在 PostRow 中创建一个按钮,它将视图从 PostView 切换到 CommentView。但是,如果我只是将 NavigationView 添加到 PostRow,我的帖子不会加载。如果我将 NavigationView 添加到我的 PostView 文档,我会遇到大量视觉故障。
我不知道如何使用此文档结构正确切换视图。
PostView.swift
import SwiftUI
struct PostView: View {
var edges = UIApplication.shared.windows.first?.safeAreaInsets
@StateObject var postData = PostViewModel()
var body: some View {
VStack{
HStack{
Text("Posts")
.font(.largeTitle)
.fontWeight(.heavy)
.foregroundColor(.white)
Spacer(minLength: 0)
.padding(.top,edges!.top)
//shadow
.background(Color("bg"))
.shadow(color: Color.white.opacity(0.06), radius: 5, x: 0, y: 5)
if postData.posts.isEmpty{
Spacer(minLength: 0)
if postData.noPosts{
Text("No Posts")
}
else{
ProgressView()
}
Spacer(minLength: 0)
}
else{
ScrollView{
VStack(spacing: 15){
ForEach(postData.posts){post in
PostRow(post: post, postData: postData)
}
}
.padding()
.padding(.bottom,55)
}
}
}
.fullScreenCover(isPresented: $postData.newPost) {
NewPost(updateId: $postData.updateId)
}
}
}
PostRow.swift - 带有按钮的文档部分
HStack {
Text(post.title)
.fontWeight(.bold)
.foregroundColor(.white)
.fixedSize(horizontal: false, vertical: true)
Spacer(minLength: 0)
}
.padding(.top,5)
.padding(.bottom,5)
HStack {
Spacer(minLength: 0)
Text(post.time,style: .time)
.font(.caption2)
.fontWeight(.light)
.foregroundColor(.white)
}
Button("Comments") {
//This is where I need to switch to the CommentsView
}
我认为这是一个相对简单的问题,我就是想不通。
谢谢!
有很多方法可以做到这一点。这是一个。
import SwiftUI
enum MainViewTypes: String{
case comments
case posts
}
struct ParentView: View {
@SceneStorage("mainViewType") var mainViewType: String = MainViewTypes.posts.rawValue
var body: some View {
switch mainViewType{
case MainViewTypes.comments.rawValue:
//Comments View
VStack{
Text("comments")
Button("go to posts", action: {
mainViewType = MainViewTypes.posts.rawValue
})
}
case MainViewTypes.posts.rawValue:
//Posts view
VStack{
Text("posts")
PostRowView()
}
default:
Button("unknown", action: {
mainViewType = MainViewTypes.posts.rawValue
})
}
}
}
struct PostRowView: View {
@SceneStorage("mainViewType") var mainViewType: String = MainViewTypes.posts.rawValue
var body: some View {
Button("go to comments", action: {
mainViewType = MainViewTypes.comments.rawValue
})
}
}
struct ParentView_Previews: PreviewProvider {
static var previews: some View {
ParentView()
}
}
我正在尝试向我的应用添加评论。为此,我显然需要切换我的应用程序的视图。目前,我有两个文件:PostView 和 PostRow。 PostView 是实际的视图文件,PostRow 是显示帖子的文件。
我正在尝试在 PostRow 中创建一个按钮,它将视图从 PostView 切换到 CommentView。但是,如果我只是将 NavigationView 添加到 PostRow,我的帖子不会加载。如果我将 NavigationView 添加到我的 PostView 文档,我会遇到大量视觉故障。
我不知道如何使用此文档结构正确切换视图。
PostView.swift
import SwiftUI
struct PostView: View {
var edges = UIApplication.shared.windows.first?.safeAreaInsets
@StateObject var postData = PostViewModel()
var body: some View {
VStack{
HStack{
Text("Posts")
.font(.largeTitle)
.fontWeight(.heavy)
.foregroundColor(.white)
Spacer(minLength: 0)
.padding(.top,edges!.top)
//shadow
.background(Color("bg"))
.shadow(color: Color.white.opacity(0.06), radius: 5, x: 0, y: 5)
if postData.posts.isEmpty{
Spacer(minLength: 0)
if postData.noPosts{
Text("No Posts")
}
else{
ProgressView()
}
Spacer(minLength: 0)
}
else{
ScrollView{
VStack(spacing: 15){
ForEach(postData.posts){post in
PostRow(post: post, postData: postData)
}
}
.padding()
.padding(.bottom,55)
}
}
}
.fullScreenCover(isPresented: $postData.newPost) {
NewPost(updateId: $postData.updateId)
}
}
}
PostRow.swift - 带有按钮的文档部分
HStack {
Text(post.title)
.fontWeight(.bold)
.foregroundColor(.white)
.fixedSize(horizontal: false, vertical: true)
Spacer(minLength: 0)
}
.padding(.top,5)
.padding(.bottom,5)
HStack {
Spacer(minLength: 0)
Text(post.time,style: .time)
.font(.caption2)
.fontWeight(.light)
.foregroundColor(.white)
}
Button("Comments") {
//This is where I need to switch to the CommentsView
}
我认为这是一个相对简单的问题,我就是想不通。
谢谢!
有很多方法可以做到这一点。这是一个。
import SwiftUI
enum MainViewTypes: String{
case comments
case posts
}
struct ParentView: View {
@SceneStorage("mainViewType") var mainViewType: String = MainViewTypes.posts.rawValue
var body: some View {
switch mainViewType{
case MainViewTypes.comments.rawValue:
//Comments View
VStack{
Text("comments")
Button("go to posts", action: {
mainViewType = MainViewTypes.posts.rawValue
})
}
case MainViewTypes.posts.rawValue:
//Posts view
VStack{
Text("posts")
PostRowView()
}
default:
Button("unknown", action: {
mainViewType = MainViewTypes.posts.rawValue
})
}
}
}
struct PostRowView: View {
@SceneStorage("mainViewType") var mainViewType: String = MainViewTypes.posts.rawValue
var body: some View {
Button("go to comments", action: {
mainViewType = MainViewTypes.comments.rawValue
})
}
}
struct ParentView_Previews: PreviewProvider {
static var previews: some View {
ParentView()
}
}