我是SWIFT的初学者,现在我面临UI的问题。让我尝试解释我的问题。
我的主视图屏幕数据来自使用Observable对象的Web服务,并且第一次加载了该数据。但是,当我尝试打开左侧幻灯片菜单时,打开左侧幻灯片菜单视图时,homeView webservice / observable对象数据只是被清除。为什么我的可观察对象数据为空。让我分享我的代码:
1 .------这是我的main / parentView
Expected spy executeRules to have been called.
2 .-----这是我的RouteChanger视图,用于导航到视图的不同页面。
struct ContentView: View {
@EnvironmentObject var viewRouter: ViewRouter
var body: some View {
let drag = DragGesture()
.onEnded {
if $0.translation.width < -100 {
withAnimation {
self.viewRouter.showSlideOutMenu = false
self.viewRouter.showDepartmentsMenu = false
self.viewRouter.showAccountMenu = false
}
}
}
return GeometryReader { g in
ZStack(alignment: .leading) {
RouteChanger(viewRouter: self._viewRouter)
if self.viewRouter.showSlideOutMenu {
MainMenuView(viewRouter: self._viewRouter)
.frame(width: g.size.width/2)
.transition(.move(edge: .leading))
}
}
.gesture(drag)
}
}
}
3 ....这是我正在使用Observable Object的homeView
struct RouteChanger: View {
@EnvironmentObject var viewRouter: ViewRouter
var body: some View {
GeometryReader { g in
VStack {
if self.viewRouter.currentPage == "Home" {
HomeView()
//.modifier(PageSwitchModifier())
}
}
}
}
}
答案 0 :(得分:1)
EnvironmentObject
会自动为所有子视图注入,因此ContentView
的相关部分应如下所示
ZStack(alignment: .leading) {
RouteChanger() // << here
if self.viewRouter.showSlideOutMenu {
MainMenuView() // << here
.frame(width: g.size.width/2)
.transition(.move(edge: .leading))
}