Swift - 解除警报消息关闭视图控制器

时间:2015-04-14 14:52:01

标签: ios xcode swift parse-platform

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

对我来说,有一件事是控制台日志中的错误,我认为它实际上与Parse if / else语句相关联,而不是与field == 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)

                }
            }


        }


    }

1 个答案:

答案 0 :(得分:3)

从“确定”按钮self.dismissViewControllerAnimated(true, completion: nil)' UIAlertAction中删除handler。单击“确定”按钮后,警报将自动被解除,您将通过此调用解除注册控制器。