从 ViewRouter 和 ObservableObjects 或 environmentObject 指向 SwiftUI 内的所有三个视图
Pointing to All Three views inside SwiftUI from a ViewRouter and ObservableObjects or environmentObject's
我最近一直在使用SwiftUI,并且我设法在ContentView.swift中写入了我的rootView,包括2个按钮可以更改为其他"pages"或"screens" 在一个应用程序中。目前,我可以在我的代码中的两个视图之间移动。我在每个 "screen" swift 文件中使用了 ObservableObjects(viewRouter : ViewRouter) 。我决定编写第三个屏幕弹出供用户查看。在包含相同的结构和方式来调出该视图之后:第三个视图的按钮操作不是 运行。 编译没有错误,应用程序将构建!但是,当点击 FirstScreen 中的按钮时,ThirdScreen 不可见。
这是主视图 ContentView:
import SwiftUI
struct ContentView: View {
@State var screen: String = "FirstScreen" //Declares the first state
@EnvironmentObject var viewRouter : ViewRouter //References our other swift file.
var body : some View {
VStack {
if viewRouter.currentScreen == "FirstScreen" {
FirstScreen()
} else if viewRouter.currentScreen == "SecondScreen" {
SecondScreen()
.transition(.slide)
} else if viewRouter.currentScreen == "ThirdScreen" {
ThirdScreen()
.transition(.opacity)
}
struct ContentView_Previews: PreviewProvider { //Looks fine, its the previews
static var previews: some View {
ContentView().environmentObject(ViewRouter())}}
这是 FirstScreen 文件,其结构如下:
import Foundation
import UIKit
import SwiftUI
struct FirstScreen : View {
@EnvironmentObject var viewRouter : ViewRouter
var body : some View {
VStack {
Button(action: {
print("The info button has been clicked.")
self.viewRouter.currentScreen = "SecondScreen" //Heres an action
}) {
Image(systemName: "info")
.padding()
.background(Color.green)
.font(.largeTitle)
.foregroundColor(Color.orange)
.frame(width: 300, height: 600)
}
Button(action: {
print("You have erased it.")
self.viewRouter.currentScreen = "ThirdScreen" //This action does not happen.
}) {
Image(systemName: "trash")
.padding()
.background(Color.red)
.font(.largeTitle)
.foregroundColor(Color.white)
.frame(width: 426, height: 620)
}}}}
struct FirstScreen_previews : PreviewProvider {
static var previews: some View {
FirstScreen().environmentObject(ViewRouter())
}
}
这是我的 ThirdScreen 的代码,它应该从 ViewRouter 加载,并通过单击 FirstScreen 的 Swift 文件中的按钮进行更改。
import Foundation
import UIKit
import SwiftUI
struct ThirdScreen : View {
@EnvironmentObject var viewRouter : ViewRouter
var body : some View {
VStack {
Text("You step a bit off balance")
.font(.largeTitle)
.foregroundColor(Color.red)
Text("You stand up, shaking, under a pass")
.font(.callout)
Text("It looks to be a wrong step. Maybe you head back?")
.font(.caption)
.foregroundColor(Color.gray)
Button(action :{self.viewRouter.currentScreen = "FirstScreen"}){
Image(systemName: "capslock" )
.frame(width: 209, height: 308)
.foregroundColor(Color.green)
}}}}
struct Third_preview : PreviewProvider {
static var previews: some View {
ThirdScreen().environmentObject(ViewRouter())
}
}
这里是 ViewRouter.swift,可以很好地管理屏幕。
import Foundation
import Combine
import SwiftUI
class ViewRouter: ObservableObject {
let objectWillChange = PassthroughSubject<ViewRouter, Never>()
var currentScreen : String = "FirstScreen" {
didSet {
withAnimation(){
objectWillChange.send(self)
}
}
}
}
最后,我的工作正常 SecondScreen.swift。这个文件可能是多余的,但我把它包括在内是因为这个文件中有一些好的东西在工作。当按钮在内部工作以更改视图时。
import Foundation
import UIKit
import SwiftUI
struct SecondScreen : View {
@EnvironmentObject var viewRouter : ViewRouter
var body : some View {
VStack {
Button(action: {self.viewRouter.currentScreen = "FirstScreen"}) {
Image(systemName: "arrowshape.turn.up.left" )
.padding()
.cornerRadius(18)
.frame(width: 180, height: 400)
}}}}
struct SecondScreen_previews : PreviewProvider{
static var previews : some View {
SecondScreen().environmentObject(ViewRouter())
}
}
非常感谢你在这里帮助我。我很乐意继续制作如下所示的屏幕文件,我只是想知道为什么我的第三个屏幕没有被拉起。在我继续解决此问题时,我会迅速做出回应。谢谢!
!解决了 运行 对代码使用其他按钮操作类型的更多测试,并且能够查看下一个屏幕。
我最近一直在使用SwiftUI,并且我设法在ContentView.swift中写入了我的rootView,包括2个按钮可以更改为其他"pages"或"screens" 在一个应用程序中。目前,我可以在我的代码中的两个视图之间移动。我在每个 "screen" swift 文件中使用了 ObservableObjects(viewRouter : ViewRouter) 。我决定编写第三个屏幕弹出供用户查看。在包含相同的结构和方式来调出该视图之后:第三个视图的按钮操作不是 运行。 编译没有错误,应用程序将构建!但是,当点击 FirstScreen 中的按钮时,ThirdScreen 不可见。
这是主视图 ContentView:
import SwiftUI
struct ContentView: View {
@State var screen: String = "FirstScreen" //Declares the first state
@EnvironmentObject var viewRouter : ViewRouter //References our other swift file.
var body : some View {
VStack {
if viewRouter.currentScreen == "FirstScreen" {
FirstScreen()
} else if viewRouter.currentScreen == "SecondScreen" {
SecondScreen()
.transition(.slide)
} else if viewRouter.currentScreen == "ThirdScreen" {
ThirdScreen()
.transition(.opacity)
}
struct ContentView_Previews: PreviewProvider { //Looks fine, its the previews
static var previews: some View {
ContentView().environmentObject(ViewRouter())}}
这是 FirstScreen 文件,其结构如下:
import Foundation
import UIKit
import SwiftUI
struct FirstScreen : View {
@EnvironmentObject var viewRouter : ViewRouter
var body : some View {
VStack {
Button(action: {
print("The info button has been clicked.")
self.viewRouter.currentScreen = "SecondScreen" //Heres an action
}) {
Image(systemName: "info")
.padding()
.background(Color.green)
.font(.largeTitle)
.foregroundColor(Color.orange)
.frame(width: 300, height: 600)
}
Button(action: {
print("You have erased it.")
self.viewRouter.currentScreen = "ThirdScreen" //This action does not happen.
}) {
Image(systemName: "trash")
.padding()
.background(Color.red)
.font(.largeTitle)
.foregroundColor(Color.white)
.frame(width: 426, height: 620)
}}}}
struct FirstScreen_previews : PreviewProvider {
static var previews: some View {
FirstScreen().environmentObject(ViewRouter())
}
}
这是我的 ThirdScreen 的代码,它应该从 ViewRouter 加载,并通过单击 FirstScreen 的 Swift 文件中的按钮进行更改。
import Foundation
import UIKit
import SwiftUI
struct ThirdScreen : View {
@EnvironmentObject var viewRouter : ViewRouter
var body : some View {
VStack {
Text("You step a bit off balance")
.font(.largeTitle)
.foregroundColor(Color.red)
Text("You stand up, shaking, under a pass")
.font(.callout)
Text("It looks to be a wrong step. Maybe you head back?")
.font(.caption)
.foregroundColor(Color.gray)
Button(action :{self.viewRouter.currentScreen = "FirstScreen"}){
Image(systemName: "capslock" )
.frame(width: 209, height: 308)
.foregroundColor(Color.green)
}}}}
struct Third_preview : PreviewProvider {
static var previews: some View {
ThirdScreen().environmentObject(ViewRouter())
}
}
这里是 ViewRouter.swift,可以很好地管理屏幕。
import Foundation
import Combine
import SwiftUI
class ViewRouter: ObservableObject {
let objectWillChange = PassthroughSubject<ViewRouter, Never>()
var currentScreen : String = "FirstScreen" {
didSet {
withAnimation(){
objectWillChange.send(self)
}
}
}
}
最后,我的工作正常 SecondScreen.swift。这个文件可能是多余的,但我把它包括在内是因为这个文件中有一些好的东西在工作。当按钮在内部工作以更改视图时。
import Foundation
import UIKit
import SwiftUI
struct SecondScreen : View {
@EnvironmentObject var viewRouter : ViewRouter
var body : some View {
VStack {
Button(action: {self.viewRouter.currentScreen = "FirstScreen"}) {
Image(systemName: "arrowshape.turn.up.left" )
.padding()
.cornerRadius(18)
.frame(width: 180, height: 400)
}}}}
struct SecondScreen_previews : PreviewProvider{
static var previews : some View {
SecondScreen().environmentObject(ViewRouter())
}
}
非常感谢你在这里帮助我。我很乐意继续制作如下所示的屏幕文件,我只是想知道为什么我的第三个屏幕没有被拉起。在我继续解决此问题时,我会迅速做出回应。谢谢!
!解决了 运行 对代码使用其他按钮操作类型的更多测试,并且能够查看下一个屏幕。