【问题标题】:why my logout button can logout but can't switch the view为什么我的注销按钮可以注销但不能切换视图
【发布时间】:2021-10-23 22:49:47
【问题描述】:

我已经设置了注销按钮。该按钮的功能是正确的,可以实际注销。但是有一个问题:当我按下注销按钮时,我无法将页面切换到我想要的视图,即LoginsignupView。我在我的代码中找不到任何错误。

我想知道如何正确制作。

这是关于注销的代码

import SwiftUI

struct MainView: View {
    @State private var selection = 0
    
    @ObservedObject private var httpClient = HTTPUser()
    @State var isin : Bool = true
    
  
    
    
    
    var body: some View {
        
        
        
        if #available(iOS 14.0, *) {
            ZStack{
                TabView(selection: $selection){
                    profile1View()
                        .tag(0)
                        .navigationBarBackButtonHidden(true)
                        .navigationBarHidden(true)
                    //List(self.httpClient.user,id: \.id){ user in
                    profile3()
                        .tag(1)
                        .navigationBarBackButtonHidden(true)
                        .navigationBarHidden(true)
                    
                    ZStack{
                        Image("background")
                            .resizable()
                            .scaledToFill()
                            .frame(minWidth: 0, maxWidth: .infinity)
                            .edgesIgnoringSafeArea(.all)
                        
                        NavigationLink(
                            destination: LoginsignupView().navigationBarBackButtonHidden(true)
                                .navigationBarHidden(true))
                        {
                            
                            Text("logout").foregroundColor(.white)
                                .fontWeight(.bold)
                                .padding(.vertical)
                                .frame(width: UIScreen.main.bounds.width - 100)
                                .background(
                                    
                                    LinearGradient(gradient: .init(colors: [Color("Color"),Color("Color1"),Color("Color2")]), startPoint: .leading, endPoint: .trailing)
                                )
                        }
                    }
                    .tag(2)
                    .onAppear(perform: httpClient.Logout)
                    .navigationBarBackButtonHidden(true)
                    .navigationBarHidden(true)
    
                }
                .zIndex(0)
                .onAppear{
                    UITabBar.appearance().barTintColor = .white
                }
            }
        }
        if #available(iOS 14.0, *) {
            TabBarView(selection: $selection)
                .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
        } else {
            // Fallback on earlier versions
        }
        
        Divider()
        
    }
}

struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        MainView()
    }
}

【问题讨论】:

标签: swift button swiftui logout


【解决方案1】:

如果意图是在按下“注销”后推送LoginSignupView,那么您需要在视图层次结构中的某处使用NavigationViewNavigationLink 本身不会做太多事情。

但是,从您的代码看来,一旦显示最后一个选项卡,您就会执行注销。在这种情况下,您可能还希望将现有的 NavigationLink 绑定到布尔属性。例如,您的标签可能如下所示:

NavigationView {
.....
                  ZStack{
                        Image("background")
                            .resizable()
                            .scaledToFill()
                            .frame(minWidth: 0, maxWidth: .infinity)
                            .edgesIgnoringSafeArea(.all)
                        
                        NavigationLink(
                            destination: LoginsignupView().navigationBarBackButtonHidden(true)
                                .navigationBarHidden(true), isActive:$showingLogout)
                        {
                            
                            Text("logout").foregroundColor(.white)
                                .fontWeight(.bold)
                                .padding(.vertical)
                                .frame(width: UIScreen.main.bounds.width - 100)
                                .background(
                                    
                                    LinearGradient(gradient: .init(colors: [Color("Color"),Color("Color1"),Color("Color2")]), startPoint: .leading, endPoint: .trailing)
                                )
                        }
                    }
                    .tag(2)
                    .onAppear(perform: {
                             httpClient.Logout()
                             showingLogout = true
                    })
                    .navigationBarBackButtonHidden(true)
                    .navigationBarHidden(true)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2012-08-25
    • 2012-03-14
    相关资源
    最近更新 更多