如何在 SwiftUI 中使用@State 和 fullScreenCover 在视图之间切换
How to switch between views using @State and fullScreenCover in SwiftUI
我正在尝试弄清楚如何在 SwiftUI 中使用 @State 和 fullScreenCover 在视图之间切换
我有一个主视图和 2 个简单视图(View1
和 View2
)。 View1
必须在点击 "Show view 1"
按钮时打开,而 View2
在 "Show view 2"
按钮上输入时必须打开。我还为此创建了 2 个 @State
变量,并使用 .View1
(ActiveFullscreenview
枚举)对其进行了初始化:
@State private var showFullscreenView = false
@State private var activeFullscreenView: ActiveFullscreenView = .View1
问题是,即使我点击 Show view 2
按钮,它也会不断显示 View1
。
我认为当点击按钮并为 activeFullscreenView 变量赋值 .View2
时,它应该更改变量并显示 fullScreenView
.
我以为是因为我调用了 showFullscreenView
并且它改变了变量,之后默认值是 .View1
所以它没有改变它。
也试过这个:
self.showFullscreenView = true
self.activeFullscreenView = .View2
第二个假设是我不应该给这个变量初始值。但在这种情况下,它添加了一些不方便的额外初始化代码:
@State private var activeFullscreenView: ActiveFullscreenView = .View1
目前我有以下代码存在所描述的问题:
import SwiftUI
struct MainView: View {
enum ActiveFullscreenView {
case View1, View2
}
@State private var showFullscreenView = false
@State private var activeFullscreenView: ActiveFullscreenView = .View1
var body: some View {
ZStack {
VStack {
// Button to show View 1 in FullScreen
Button("Show view 1", action: {
self.activeFullscreenView = .View1
self.showFullscreenView = true
})
// Button to show View 2 in FullScreen
Button("Show view 2", action: {
self.activeFullscreenView = .View2
self.showFullscreenView = true
})
}
}
.fullScreenCover(isPresented: $showFullscreenView) {
switch self.activeFullscreenView {
case .View1:
Text("View1")
case .View2:
Text("View2")
}
}
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
}
}
感谢任何与解决问题相关的帮助。关于如何使用 fullscreenCover 实现显示单独视图的相同结果的便捷方法表示赞赏。
工作于 Xcode 12.4,最新 iOS 14
在@jnpdx 提供的帮助下
问题解决。这是代码。只要 fullScreenCover 和 sheet 都是模态,我将枚举重命名为 ModalView:
struct MainView: View {
// enum to decide which view to present:
enum ModalView: String, Identifiable { // Identifiable
case View1, View2
var id: String {
return self.rawValue
}
}
@State var activeModalView : ModalView? = nil
var body: some View {
ZStack {
VStack {
// Button to show View 1
Button("Show view 1", action: {
self.activeModalView = .View1
})
// Button to show View 2
Button("Show view 2", action: {
self.activeModalView = .View2
})
}
} // use fullScreenCover or sheet depending on your needs
.fullScreenCover(item: $activeModalView) { activeModalValue in
switch activeModalValue {
case .View1:
Text("View1")
case .View2:
Text("View2")
}
}
}
}
我正在尝试弄清楚如何在 SwiftUI 中使用 @State 和 fullScreenCover 在视图之间切换
我有一个主视图和 2 个简单视图(View1
和 View2
)。 View1
必须在点击 "Show view 1"
按钮时打开,而 View2
在 "Show view 2"
按钮上输入时必须打开。我还为此创建了 2 个 @State
变量,并使用 .View1
(ActiveFullscreenview
枚举)对其进行了初始化:
@State private var showFullscreenView = false
@State private var activeFullscreenView: ActiveFullscreenView = .View1
问题是,即使我点击 Show view 2
按钮,它也会不断显示 View1
。
我认为当点击按钮并为 activeFullscreenView 变量赋值 .View2
时,它应该更改变量并显示 fullScreenView
.
我以为是因为我调用了 showFullscreenView
并且它改变了变量,之后默认值是 .View1
所以它没有改变它。
也试过这个:
self.showFullscreenView = true
self.activeFullscreenView = .View2
第二个假设是我不应该给这个变量初始值。但在这种情况下,它添加了一些不方便的额外初始化代码:
@State private var activeFullscreenView: ActiveFullscreenView = .View1
目前我有以下代码存在所描述的问题:
import SwiftUI
struct MainView: View {
enum ActiveFullscreenView {
case View1, View2
}
@State private var showFullscreenView = false
@State private var activeFullscreenView: ActiveFullscreenView = .View1
var body: some View {
ZStack {
VStack {
// Button to show View 1 in FullScreen
Button("Show view 1", action: {
self.activeFullscreenView = .View1
self.showFullscreenView = true
})
// Button to show View 2 in FullScreen
Button("Show view 2", action: {
self.activeFullscreenView = .View2
self.showFullscreenView = true
})
}
}
.fullScreenCover(isPresented: $showFullscreenView) {
switch self.activeFullscreenView {
case .View1:
Text("View1")
case .View2:
Text("View2")
}
}
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
}
}
感谢任何与解决问题相关的帮助。关于如何使用 fullscreenCover 实现显示单独视图的相同结果的便捷方法表示赞赏。
工作于 Xcode 12.4,最新 iOS 14
在@jnpdx 提供的帮助下
struct MainView: View {
// enum to decide which view to present:
enum ModalView: String, Identifiable { // Identifiable
case View1, View2
var id: String {
return self.rawValue
}
}
@State var activeModalView : ModalView? = nil
var body: some View {
ZStack {
VStack {
// Button to show View 1
Button("Show view 1", action: {
self.activeModalView = .View1
})
// Button to show View 2
Button("Show view 2", action: {
self.activeModalView = .View2
})
}
} // use fullScreenCover or sheet depending on your needs
.fullScreenCover(item: $activeModalView) { activeModalValue in
switch activeModalValue {
case .View1:
Text("View1")
case .View2:
Text("View2")
}
}
}
}