2015-04-14 94 views
1

我有一个注册过程,我有一个登录/注册Facebook(连接到分析)的入口点。如果用户从未注册过他们的Facebook帐户,那么他们将被发送到用户注册用户名,电子邮件和密码的发送页面。我有一个功能设置,如果用户将任何文本字段留空以供用户注册,则会显示警告消息,并显示错误消息,指出该字段为空。此功能正常工作,但当单击“确定”关闭消息时,注册视图控制器将自行关闭,并显示入口点(登录屏幕)视图控制器。这不应该发生,我没有从登记屏幕登录屏幕的segue设置。有什么想法吗?Swift - 关闭警报消息关闭视图控制器

弹出给我的一件事是控制台日志中的错误,我相信它实际上与Parse if/else语句相关联,而不是与字段== nil语句相关联。

控制台登录:

2015-04-14 10:42:56.293 tappery[574:142525] [Error]: missing username (Code: 200, Version: 1.6.3) 

登录屏幕视图控制器:

import UIKit 

class LoginViewController: UIViewController { 


    @IBOutlet var loginCancelledLabel: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     var currentUser = PFUser.currentUser() 

     if currentUser != nil { 
      println("User is Logged in") 
     } else { 
      println("User is not logged in") 
     } 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    @IBAction func facebookLoginButton(sender: AnyObject) { 

     var permissions = ["public_profile", "email", "user_friends"] 

     self.loginCancelledLabel.alpha = 0 

     PFFacebookUtils.logInWithPermissions(permissions, { 
      (user: PFUser!, error: NSError!) -> Void in 
      if let user = user { 
       if user.isNew { 
        println("User signed up and logged in through Facebook!") 

        self.performSegueWithIdentifier("registerUser", sender: self) 

       } else { 
        println("User logged in through Facebook!") 

        self.performSegueWithIdentifier("loginSuccessful", sender: self) 

       } 
      } else { 
       println("Uh oh. The user cancelled the Facebook login.") 

       self.loginCancelledLabel.alpha = 1 

      } 
     }) 


    } 



} 

注册视图控制器:

import UIKit 

class UserRegistrationViewController: UIViewController { 


    func displayAlert(title:String, error:String) { 

     var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert) 

     alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { 
      action in 

      self.dismissViewControllerAnimated(true, completion: nil) 


     })) 

     self.presentViewController(alert, animated: true, completion: nil) 


    } 

    @IBOutlet var usernameTextField: UITextField! 

    @IBOutlet var emailTextField: UITextField! 

    @IBOutlet var passwordTextField: UITextField! 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    @IBAction func registerUser(sender: AnyObject) { 

     var error = "" 

     if usernameTextField.text == nil || emailTextField.text == nil || passwordTextField.text == nil { 

      error = "Please enter a username, email and password" 

     } 


     if error != "" { 

      displayAlert("Error In Form", error: error) 

     } else { 

      var user = PFUser.currentUser() 

      user.username = usernameTextField.text 
      user.password = passwordTextField.text 
      user.email = emailTextField.text 

      user.saveInBackgroundWithBlock { 
       (succeeded: Bool!, signupError: NSError!) -> Void in 
       if signupError == nil { 

        println(user.username) 
        println(user.password) 
        println(user.email) 


        self.performSegueWithIdentifier("successfulRegistration", sender: self) 

        // Hooray! Let them use the app now. 
       } else { 

        if let errorString = signupError.userInfo?["error"] as? NSString { 
         error = errorString 
        } else { 

         error = "Please try again later." 

        } 


        self.displayAlert("Could Not Sign Up", error: error) 

       } 
      } 


     } 


    } 

回答

2

从您的OK按钮UIAlertActionhandler删除self.dismissViewControllerAnimated(true, completion: nil)。警报在点击确定按钮后自动解除,并且您将通过此呼叫解除注册控制器。