关闭工作表后的SwiftUI Reload View

时间:2020-02-07 10:02:05

标签: ios xcode swiftui

我在UserDefaults中有一个bool值,以检查用户是否登录。 在我的第一页中,我有一个页眉,当用户单击个人资料图片时,该页眉会显示一个模式(使用.sheet),并且在其中有一个供用户注销的按钮。

当用户按下LogOut按钮并退出模式时,如何更改视图?

或者在用户单击LogOut按钮并关闭模式以检查新的Bool值后,如何重新加载主页?

struct Home: View {

    var body: some View {
        ScrollView {
            VStack {   
                // MARK: Header
                Header(title: "Home")    
            }
            Spacer()
        }
        .padding(20)
    }
}

struct Header: View {

    public var title: String = ""
    @State private var showProfile = false

    var body: some View {
        HStack {
            // MARK: - User Profile
            Image("profile")
                .resizable()
                .clipShape(Circle())
                .frame(width: 50, height: 50)
                .onTapGesture {
                    print("⚠️ Profile has been tapped.")
                    self.showProfile.toggle()
            }
            .sheet(isPresented: $showProfile) {
                Account(showProfile: self.$showProfile)
            }

        }
    }
}

struct Account: View {

    @Binding var showProfile: Bool

    var body: some View {
        NavigationView {

            // MARK: - Account Sections
            List {

                // MARK: - Log Out -
                Section {
                    Button(action: {
                        print("❌ User has been logged out.")
                        self.showProfile.toggle()

                    }) {
                        Text("Log Out")
                            .bold()
                            .foregroundColor(.red)
                    }
                }
            }

                // MARK: - NavigationBar Settings
                .navigationBarTitle("Account", displayMode: .inline)
        }
    }
}

这是我的UserDefault类

class UsersSettings: ObservableObject {

    @Published var isLoggedIn: Bool = UserDefaults.standard.bool(forKey: "isLogged") {
        didSet {
            UserDefaults.standard.set(self.isLoggedIn, forKey: "isLogged")
            print(UserDefaults.standard.bool(forKey: "isLogged"))
        }
    }
}

3 个答案:

答案 0 :(得分:0)

很难说,你的目标是什么。也许这会满足您的要求 1)当用户点击图像时,出现工作表 2)用户可以登录或注销,也可以通过向下滑动手势简单地关闭工作表 3)用户图像反映登录/退出状态

import SwiftUI

class LoginModel: ObservableObject {
    @Published var loggedIn = false
}

let loginModel = LoginModel()

struct ContentView: View {
    @ObservedObject var model = loginModel
    var body: some View {
        ScrollView {
            VStack {
                // MARK: Header
                Header(title: "Home", loggedIn: model.loggedIn)
            }
            Spacer()
        }
        .padding(20)
    }
}

struct Header: View {

    public var title: String = ""
    var loggedIn: Bool
    @State private var showProfile = false
    var body: some View {
        HStack {
            // MARK: - User Profile
            Image(systemName: loggedIn ? "photo.fill.on.rectangle.fill" : "photo.on.rectangle")
                .resizable()
                .clipShape(Circle())
                .frame(width: 50, height: 50)
                .onTapGesture {
                    self.showProfile.toggle()
            }
            .sheet(isPresented: $showProfile) {
                Account(showProfile: self.$showProfile)
            }

        }
    }
}

struct Account: View {

    @Binding var showProfile: Bool
    @ObservedObject var model = loginModel

    var body: some View {
        NavigationView {
            List {
                Section {
                    Button(action: {
                        self.model.loggedIn.toggle()
                        self.showProfile.toggle()

                    }) {
                        Text(model.loggedIn ? "Log Out": "Log In")
                            .bold()
                            .foregroundColor(.red)
                    }
                }
            }
            .navigationBarTitle("Account", displayMode: .inline)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

结果看起来像

enter image description here

答案 1 :(得分:0)

return SingleChildScrollView( child: AlertDialog( title: alertTitle, content: Column( mainAxisSize: MainAxisSize.min, children: <Widget> [ DropdownButton<String>( ....... ), TextField( autofocus: true, ....... ), ], ), ) ); 是真理的源头,必须在工作表模式视图(UserSettings)中使用。

在“主页”或“标题”视图中为此对象添加绑定,以观察更改并相应地更新界面。

您这样声明一个对象绑定:

Account

然后您观察到这样的变化:

struct Home: View {
  @ObservedObject var userSettings: UserSettings
  // ...
}

要使其正常工作,您必须从活动视图中注销用户。

答案 2 :(得分:0)