我的应用程序的初始视图控制器是HomeView,它显示对用户重要的信息。如果用户未登录,则用户界面将显示LoginView视图控制器,提示用户登录。
问题是,当用户输入错误的登录凭据,点击登录时,应用程序会显示错误。一旦错误被清除,应用程序将解除LoginView,然后再次显示LoginView。行为应该只是警报然后离开LoginView。
1)从HomeView(初始视图控制器)
override func viewWillAppear(animated: Bool) {
if (PFUser.currentUser()?.username == nil) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("LoginView")
//self.showViewController(viewController, sender: self)
self.presentViewController(viewController, animated: true, completion: nil)
})
}
}
2)从LoginView验证用户凭据,然后使用解析登录尝试。
@IBAction func loginButton(sender: AnyObject) {
let username = usernameField.text?.lowercaseString
let password = passwordField.text
if username == "" || password == "" {
displayAlert("Error in form", message: "Please enter a username and password")
} else if username?.characters.count < 5 {
self.displayAlert("Failed Signup", message: "Username must be greater than 5 characters")
} else if password?.characters.count < 8 {
self.displayAlert("Failed Signup", message: "Password must be greater than 8 characters")
} else {
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
PFUser.logInWithUsernameInBackground(username!, password: password!, block: { (user, error) -> Void in
//Kill spinner
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if ((user) != nil) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("HomeView")
self.presentViewController(viewController, animated: true, completion: nil)
})
} else {
self.displayAlert("Failed Signup", message: "\(error)")
}
})
}
}
编辑:
3)displayAlert Function
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) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})))
self.presentViewController(alert, animated: true, completion: nil)
}
我应该注意到我的家庭视图控制器是带有侧边栏菜单的SWRevealViewController的一部分。 SWRevealViewController的导航控制器作为初始视图控制器
答案 0 :(得分:1)
您需要在完成按钮时删除对self.dismissViewController
的调用。这就是告诉应用程序解除登录视图的原因。在设计仅出现错误并且您希望关闭按钮删除警报的警报时,您不必在完成时放置任何代码。