gpt4 book ai didi

swift - 用户登录到不同的 View Controller 后刷新主视图 Controller

转载 作者:行者123 更新时间:2023-11-30 12:56:18 24 4
gpt4 key购买 nike

我有一个带有按钮的主视图 Controller ,允许用户“登录”,一旦他们按下按钮,它就会加载一个处理登录的不同 View 。我的登录 View Controller 处理所有检查并查询我的 mysql 数据库以进行确保用户存在并且有效。一旦用户有效,我如何刷新已加载的主视图 Controller ,以便它收集我的用户信息。例如,现在我希望主 Controller 上的“登录”按钮显示“登录”,而不是显示他们的电子邮件。我传递此信息没有问题,但如何刷新我的主视图 Controller ,以便它现在知道用户已从不同的 View Controller 登录。

LoginViewController

if(resultValue == "Success"){

UserDefaults.standard.set(true, forKey: "isLoggedIn");
UserDefaults.standard.synchronize();

}

MainViewController

override func viewWillAppear() {

super.viewWillAppear()

let isloggedin = UserDefaults.standard.bool(forKey: "isLoggedIn")

if (isloggedin == true){

SignInButton.title = "Email"


}else{

SignInButton.title = "Sign In"
}
}

最佳答案

如果您需要按照上面的建议“发送”信息到特定接收者(MainViewController),您可以使用委托(delegate)或关闭机制(否则可以使用通知机制)。请参阅下面的关闭说明。

注意该行闭包中的“lvc.action = {() in print(self)}”行确保对 self 的引用传输到 LoginVC,因此尽管代码在 LoginVC 中运行,打印结果是主VC。希望有帮助。

代码可能不是最佳的,但表明了要点。这是(Xcode 8.1(8B62))的完整说明:删除Info.plist中的 Storyboard,启动屏幕,清理属性主 Storyboard和启动屏幕,并将AppDelegate.swift替换为以下内容

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.rootViewController = MainViewController()
self.window!.makeKeyAndVisible()
return true
}
}

class LoginViewController: UIViewController {
var action: (()->())!
override func viewDidLoad() {
super.viewDidLoad()
let b = self.view.addSubviewWithConstraints(["b" : Button(title: "Validate user")], constraints: ["V:|-10-[b(70)]", "H:|-10-[b]-10-|"])["b"]
(b as! Button).action = self.action

}
}

class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let b = self.view.addSubviewWithConstraints(["b" : Button(title: "Log in")], constraints: ["V:|-10-[b(70)]", "H:|-10-[b]-10-|"])["b"]
(b as! Button).action = {
let lvc = LoginViewController()
lvc.action = {() in print(self)}
self.present(lvc, animated: false,completion: nil)
}
}
}

class Button: UIButton {
var action: (()->())!
init(title: String) {
super.init(frame : CGRect.zero)
self.setTitle(title, for: UIControlState())
self.addTarget(self, action: #selector(Button.buttonAction(_:)), for: .touchUpInside)
}
required init?(coder: NSCoder) {super.init(coder: coder)}
func buttonAction(_ sender: AnyObject) {if action != nil {action()}}
}

extension UIView {
func addSubviewWithConstraints(_ views: [String : AnyObject], constraints: Array<String>) -> [String : AnyObject] {
for (_, view) in views {
self.addSubview(view as! UIView)
(view as! UIView).translatesAutoresizingMaskIntoConstraints = false
}
for i in 0 ..< constraints.count {self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: constraints[i], options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))}
return views
}
}

关于swift - 用户登录到不同的 View Controller 后刷新主视图 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40365165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com