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

Posted

技术标签:

【中文标题】Swift - 关闭警报消息关闭视图控制器【英文标题】:Swift - Dismissing Alert Message Closes View Controller 【发布时间】:2015-04-14 14:52:00 【问题描述】:

我有一个注册过程,其中我有一个登录/注册 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】:

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

【讨论】:

以上是关于Swift - 关闭警报消息关闭视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

从已关闭的视图控制器中显示警报

如何在 SWIFT 中不执行任何操作就关闭 UIAlertController?

关闭对象引用不可用的控制器或应用程序的警报视图..!

关闭自定义警报视图并解除 segue 问题?

Swift:关闭所有视图控制器,然后呈现一个视图控制器

iOS 应用程序在警报视图关闭时崩溃