0

我试图弄清楚如何在 SwiftUI 中使用 @State 和 fullScreenCover 在视图之间切换

我有一个主视图和 2 个简单视图(View1View2)。View1必须在点击"Show view 1"按钮时以及View2在按钮上键入时"Show view 2"打开。我还@State为此创建了 2 个变量,并使用.View1ActiveFullscreenview枚举)对其进行了初始化:

 @State private var showFullscreenView = false
 @State private var activeFullscreenView: ActiveFullscreenView = .View1

问题是View1即使我点击Show view 2按钮它也会不断显示。我认为当点击按钮并给 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

4

1 回答 1

0

在@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")
                
            }
        }
        
    }
    
}
于 2021-02-12T23:01:00.183 回答