我有以下问题。几个星期后我尝试学习编码,我的实际项目是登录页面。 这个概念如下: 当我打开应用程序时,有登录屏幕,用户可以登录。这项工作到目前为止。登录后,用户将被定向到另一个视图。有一个注销按钮。这就是问题所在。当我单击该按钮时,用户将被注销(我通过打印出当前用户来证明这一点并获得了nil)并再次返回到登录屏幕。但是,当他现在点击没有或有错误数据的登录按钮时,他再次进入屏幕,尽管它出现了错误信息。 我的错在哪里? 这是我的代码:
import UIKit
import Parse
class ViewController: UIViewController {
func displayAlert(title:String, message:String){
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in self.dismiss(animated: true, completion: nil)}))
self.present(alert, animated: true, completion: nil)
}
@IBOutlet weak var benutzer: UITextField!
@IBOutlet weak var passwort: UITextField!
@IBAction func ButtonLogin(_ sender: Any) {
// Hier die Loginfunktion
let activityIndiaktor = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
activityIndiaktor.center = self.view.center
activityIndiaktor.hidesWhenStopped = true
activityIndiaktor.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
view.addSubview(activityIndiaktor)
activityIndiaktor.startAnimating()
UIApplication.shared.beginIgnoringInteractionEvents()
PFUser.logInWithUsername(inBackground: benutzer.text!, password: passwort.text!, block: { (user, error) in
if user == nil {
var errortext = "Unknown Error! Try again!"
if let error = error {
errortext = error.localizedDescription
}
self.displayAlert(title: "Login error", message: errortext)
}else{
print("Login done!")
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Main") as! UIViewController
self.present(viewController, animated: true, completion: nil)
// UIApplication.shared.endIgnoringInteractionEvents()
// activityIndiaktor.stopAnimating()
}
activityIndiaktor.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
return
})
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这里是另一种观点:
import UIKit
import Parse
class MainViewController: UIViewController {
@IBAction func Logout(_ sender: Any) {
PFUser.logOut()
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginSeite") as! UIViewController
self.present(viewController, animated: true, completion: nil)
}
@IBOutlet weak var LabelWillkommen: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let user = String(describing: PFUser.current()!.username!)
LabelWillkommen.text = "Welcome \(user)!"
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
在MainViewController
只需使用以下代码段
@IBAction func Logout(_ sender: Any) {
PFUser.logOut()
if PFUser.current()?.sessionToken == nil {
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginSeite") as! UIViewController
self.present(viewController, animated: true, completion: nil)
}
}
希望这会对你有所帮助
答案 1 :(得分:0)
我再次检查了一切。我找不到错误。 Logout工作,但if情况仍然认为,用户已登录。我可以重新加载View或重新加载if语句或类似的东西吗?还是其他想法?