我正在使用XCode 6上的框架PARSE设置注册/登录页面。
当我尝试执行segue(拼写正确)时,悬停,应用程序崩溃,即使segue在if语句中。
以下是代码:
import UIKit
import Parse
class ViewController: UIViewController, UINavigationControllerDelegate{
var signUpMode = false
func displayAlert(title:String, message:String){
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
//Outlet and actions
@IBOutlet var username: customTextField!
@IBOutlet var email: customTextField!
@IBOutlet var password: customTextField!
//Need the outlets for changes betweeen signUp and logIn modes!!!
@IBAction func signUp(sender: AnyObject) {
if signUpMode == true {
var user = PFUser()
user.username = username.text
user.password = password.text
user.email = email.text
// other fields can be set just like with PFObject
//user["phone"] = "415-392-0202"
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if error == nil {
// Hooray! Let them use the app now.
} else {
println("error")
self.displayAlert("Username already in use", message: "Please use another username")
}
}
}
else {
PFUser.logInWithUsernameInBackground(email.text, password:password.text) {
(user: PFUser!, error: NSError!) -> Void in
if user != nil {
self.displayAlert("You're in", message: "And you'll be successful")
self.performSegueWithIdentifier("goToPost", sender: self)
} else {
self.displayAlert("Wrong username or password", message: "Please try again")
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
if signUpMode == false {
self.username.hidden = true
self.email.placeholder = "username"
}
}
override func viewDidAppear(animated: Bool) {
if PFUser.currentUser() != nil {
performSegueWithIdentifier("goToPost", sender: self)
}
}
}
segue位于viewWillAppear
方法内。
PFUser().currentUser()
存储有关当前登录用户的信息,如果没有用户登录则为nil
。
你能找出崩溃的原因吗?
我试图把segue放在viewDidLoad
里面,但没有别的,它甚至没有崩溃。
答案 0 :(得分:0)
在viewDidAppear中尝试segue:并检查你的segue标识符是否与故事板上的标识符匹配。