我最近一直在使用 SwiftUI,并且我设法在 ContentView.swift 中编写了我的 rootView,包括 2 个按钮来更改应用程序中的其他“页面”或“屏幕”。目前,我可以在我的代码中的两个视图之间移动。我在每个“屏幕”快速文件中使用了 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())
}
}
非常感谢你在这里帮助我。我很想继续制作屏幕文件,如下所示,我只是想知道为什么我的第三个屏幕没有拉起。在继续解决此问题时,我将迅速做出回应。谢谢!