2015-04-14 131 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)。警報在點擊確定按鈕後自動解除,並且您將通過此呼叫解除註冊控制器。