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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29630585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:59:40  来源:igfitidea点击:

Swift - Dismissing Alert Message Closes View Controller

iosxcodeswiftparse-platform

提问by cphill

I have a registration process where I have an entry point with a Login/register with Facebook (Connected to Parse). If the user has never registered with their Facebook account, then they are sent to a send page where a user registers a username, email and password. I have a function setup that if a user leaves any of the text fields blank for the user registration, then a alert message appears with an error stating the field is blank. This functionality works correctly, but when I click "OK" to dismiss the message, the registration view controller dismisses itself and the entry point (login screen) view controller is displayed. This should not be happening and I don't have a segue setup to go from registration screen to login screen. Any thoughts?

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

One thing that pops out to me is the error in the console log, which I believe is actually associated with the Parse if/else statement, and not with the field == nil statement.

我突然想到的一件事是控制台日志中的错误,我认为这实际上与 Parse if/else 语句相关,而不是与 field == nil 语句相关联。

Console Log:

控制台日志:

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

Login Screen View Controller:

登录屏幕视图控制器:

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

            }
        })


    }



}

Registration View Controller:

注册视图控制器:

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)

                }
            }


        }


    }

回答by zrzka

Remove self.dismissViewControllerAnimated(true, completion: nil)from your OK button UIAlertAction's handler. Alert is dismissed automatically upon OK button click and you're dismissing registration controller with this call.

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