我的想法是创建一个登录应用程序,当我登录时,我会转到一个新的视图控制器并做一些事情,我的代码是这样的:
@IBAction func signUp(sender: AnyObject) {
var error = ""
if username.text == "" || password.text == "" {
error = "Please enter a username and password"
}
if error != "" {
displayAlert("Error in form", error: error)
} else {
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50)) //waiting animation
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
if signupActive == true {
var user = PFUser()
user.username = username.text
user.password = password.text
user.signUpInBackgroundWithBlock {
(succeeded: Bool, signupError: NSError?) -> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if signupError == nil {
println("signed up")
} else {
if let errorString = signupError!.userInfo?["error"] as? NSString
{
error = errorString as! String
} else {
error = "Please try again later"
}
self.displayAlert("Could not sign up", error: error)
}
}
} else {
PFUser.logInWithUsernameInBackground(username.text as String!, password:password.text as String!) {
(user: PFUser?, signupError: NSError?) -> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if signupError == nil {
println("logged in")
self.performSegueWithIdentifier("push", sender: nil)
} else {
if let errorString = signupError!.userInfo?["error"] as? NSString {
// Update - added as! String
error = errorString as! String
} else {
error = "Please try again later."
}
self.displayAlert("Could Not Log In", error: error)
}
}
}
}
}
我将注册和登录放在同一个按钮中,然后使用signUpActive
将它们分开,我将self.performSegueWithIdentifier("push", sender: nil)
放入登录部分,所以当我输入时正确的密码和用户名我可以转到新的视图控制器。
效果很好。但是当我在新的视图控制器中添加一个新按钮,当我点击该按钮时,我将转到第一个视图控制器并使用PFuser.logout()
注销,我需要重新输入正确的用户名和密码。
然后出现一个问题,即使我输入了错误的用户名,它也会转到第二个视图控制器,尽管它会显示一个警告,说明用户名无效。同时,即使我点击注册没有登录,它也会消失。为什么会这样?任何人都可以帮助我吗?
答案 0 :(得分:3)