成功注册后查看下一个视图控制器

时间:2015-09-30 03:04:56

标签: ios swift parse-platform

我已搜索,搜索和搜索但未找到解决方案。注册有效,但不会转到下一个视图控制器。继承我的代码:

import UIKit
import Parse
import Bolts

class SignUpVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var profilePictureIV: UIImageView!
@IBOutlet weak var firstNameTF: UITextField!
@IBOutlet weak var lastNameTF: UITextField!
@IBOutlet weak var newUsernameTF: UITextField!
@IBOutlet weak var newPasswordTF: UITextField!
@IBOutlet weak var emailTF: UITextField!
@IBOutlet weak var phoneNumberTF: UITextField!

@IBAction func setProfilePicture(sender: AnyObject) {
    let myPickerController = UIImagePickerController()
    myPickerController.delegate = self
    myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    self.presentViewController(myPickerController, animated: true, completion: nil)
}

@IBAction func signUpButton(sender: AnyObject) {
    let spinner: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0, 0, 150, 150)) as UIActivityIndicatorView

    if firstNameTF.text != "" &&  lastNameTF.text != "" && newUsernameTF.text != "" && newPasswordTF.text != "" && emailTF.text != "" && phoneNumberTF.text != "" {

        let newUser = PFUser()

        if let profilePictureImage = profilePictureIV?.image {
            let profilePicture = UIImageJPEGRepresentation(profilePictureImage, 1)!
            let profilePictureImageFile = PFFile(data: profilePicture)
            newUser["profilePicture"] = profilePictureImageFile
        }
        newUser["firstName"] = firstNameTF.text
        newUser["lastName"] = lastNameTF.text
        newUser.username = newUsernameTF.text
        newUser.password = newPasswordTF.text
        newUser.email = emailTF.text
        newUser["phoneNumber"] = phoneNumberTF.text

        newUser.signUpInBackgroundWithBlock {
            (succeeded: Bool, error: NSError?) -> Void in
            if let error = error {
                spinner.startAnimating()
                self.alert("Opps", textMessage: (error.localizedDescription))
            } else {
                spinner.startAnimating()
                self.alert("Congratualtion!", textMessage: "Success, your account has been created.")
                self.performSegueWithIdentifier("showMessages", sender: self)
            }
        }
    } else {
        alert("Hmm...", textMessage: "If you want an account, please fill in the blanks.")
    }
}

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.
}

override func viewWillAppear(animated: Bool) {
    self.navigationController!.navigationBar.hidden = false
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    profilePictureIV.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    self.dismissViewControllerAnimated(true, completion: nil)
}

func alert(textTitle: String, textMessage: String) {
    let alertController = UIAlertController(title: textTitle, message: textMessage, preferredStyle: .Alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
    self.presentViewController(alertController, animated: true, completion: nil)
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

如果我在其他任何地方播放self.performSegueWithIdentifier("showMessages", sender: self)行,那么当我运行程序并按下注册按钮时,它将直接进入消息视图控制器,而无需任何注册。

2 个答案:

答案 0 :(得分:0)

正如Paulw11所说,尝试删除警告信息: -

       if let error = error {
            spinner.startAnimating()
            self.alert("Opps", textMessage: (error.localizedDescription))
        } else {
            spinner.startAnimating()
            self.performSegueWithIdentifier("showMessages", sender: self)

          }

或者您可以在代码中调用处理程序。

          if let error = error {
            spinner.startAnimating()
            self.alert("Opps", textMessage: (error.localizedDescription))
        } else {
            spinner.startAnimating()
            var alert = UIAlertController(title: "Welcome", message: "Login", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title:"OK", style: .Default, handler:  { action in self.performSegueWithIdentifier("showMessages", sender: self) }

          }

答案 1 :(得分:-1)

你不能将自己作为一个参数传递给该区块。

所以创建一个方法来调用segue

@IBAction func signUpButton(sender: AnyObject) {
  let spinner: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0, 0, 150, 150)) as UIActivityIndicatorView

if firstNameTF.text != "" &&  lastNameTF.text != "" && newUsernameTF.text != "" && newPasswordTF.text != "" && emailTF.text != "" && phoneNumberTF.text != "" {

    let newUser = PFUser()

    if let profilePictureImage = profilePictureIV?.image {
        let profilePicture = UIImageJPEGRepresentation(profilePictureImage, 1)!
        let profilePictureImageFile = PFFile(data: profilePicture)
        newUser["profilePicture"] = profilePictureImageFile
    }
    newUser["firstName"] = firstNameTF.text
    newUser["lastName"] = lastNameTF.text
    newUser.username = newUsernameTF.text
    newUser.password = newPasswordTF.text
    newUser.email = emailTF.text
    newUser["phoneNumber"] = phoneNumberTF.text

    newUser.signUpInBackgroundWithBlock {
        (succeeded: Bool, error: NSError?) -> Void in
        if let error = error {
            spinner.startAnimating()
            self.alert("Opps", textMessage: (error.localizedDescription))
        } else {
            spinner.startAnimating()
            self.alert("Congratualtion!", textMessage: "Success, your account has been created.")
            callSegue()
        }
    }
} else {
    alert("Hmm...", textMessage: "If you want an account, please fill in the blanks.")
}
}

func callSegue()
{
   self.performSegueWithIdentifier("showMessages", sender: self)
}