SwiftUI:使用“if else”更改视图
Posted
技术标签:
【中文标题】SwiftUI:使用“if else”更改视图【英文标题】:SwiftUI: changing view using "if else" 【发布时间】:2020-05-30 01:47:19 【问题描述】:我正在尝试根据使用按钮进行的选择来更改视图。我正在使用 Combine 和 SwiftUI。
我创建了一个带有@Published
var: 的视图路由器类:
import Foundation
import SwiftUI
import Combine
class ViewRouter: ObservableObject
@Published var currentView = "folder"
我有我的按钮结构:
import SwiftUI
import Combine
struct MultiButton: View
@ObservedObject var viewRouter = ViewRouter()
@State var showButton = false
var body: some View
GeometryReader geometry in
ZStack
// open multi button
Button(action:
withAnimation
self.showButton.toggle()
)
Image(systemName: "gear")
.resizable()
.frame(width: 40, height: 40, alignment: .center)
if self.showButton
Multi()
.offset(CGSize(width: -30, height: -100))
struct MultiButton_Previews: PreviewProvider
static var previews: some View
MultiButton()
struct Multi: View
@ObservedObject var viewRouter = ViewRouter()
var body: some View
HStack(spacing: 10)
ZStack
Button(action:
self.viewRouter.currentView = "folder"
debugPrint("\(self.viewRouter.currentView)")
)
ZStack
Circle()
.foregroundColor(Color.blue)
.frame(width: 70, height: 70)
Image(systemName: "folder")
.resizable()
.aspectRatio(contentMode: .fit)
.padding(20)
.frame(width: 70, height: 70)
.foregroundColor(.white)
.shadow(radius: 10)
Button(action:
self.viewRouter.currentView = "setting"
debugPrint("\(self.viewRouter.currentView)")
, label:
ZStack
Circle()
.foregroundColor(Color.blue)
.frame(width: 70, height: 70)
Image(systemName: "gear")
.resizable()
.aspectRatio(contentMode: .fit)
.padding(20)
.frame(width: 70, height: 70)
.foregroundColor(.white)
.shadow(radius: 10)
)
.transition(.scale)
还有contentView
:
import SwiftUI
import Combine
struct ContentView: View
@ObservedObject var viewRouter = ViewRouter()
var body: some View
VStack
MultiButton()
if viewRouter.currentView == "folder"
Text("folder")
else if viewRouter.currentView == "setting"
Text("setting")
为什么按下按钮时我的文本没有从文件夹更改为设置基础?
变量是@Published
;它应该发布更改并更新主视图。
我错过了什么吗?
【问题讨论】:
【参考方案1】:您有多个 ViewRouter
实例,每个视图都有自己的实例。如果您更改该实例,则其他实例不会更改。
要解决这个问题,您要么必须手动传递一个实例:
struct MultiButton
@ObservedObject var viewRouter: ViewRouter
...
struct ContentView
@ObservedObject var viewRouter = ViewRouter()
var body: some View
VStack
MultiButton(viewRouter: self.viewRouter)
...
或者您可以将其声明为环境对象:
struct MultiButton
@EnvironmentObject var viewRouter: ViewRouter
...
然后注入它,例如当您在场景委托中创建视图层次结构时:
let contentView = ContentView()
.environmentObject(ViewRouter())
请注意,在场景委托中注入环境对象时,它不会在使用 ViewRouter 的视图的每个预览中都可用,除非您也手动将其注入那里:
static var previews: some View
ContentView()
.environmentObject(ViewRouter())
【讨论】:
以上是关于SwiftUI:使用“if else”更改视图的主要内容,如果未能解决你的问题,请参考以下文章