2015-08-18 162 views
0

我正在使用performSegueWithIdentifier並且所有這些設備都可以正常工作,但有一個根本不起作用。performSegueWithIdentifier註冊後解析不起作用

我正在使用Parse.com來註冊用戶,並且我想在他們完成升級後執行一個segue,但是我無法使其工作。我認爲performSegue應在功能是地方signUpViewController

enter image description here

我的代碼:

import UIKit 
import Parse 
import ParseUI 
import Bolts 
import FBSDKCoreKit 
import FBSDKLoginKit 
import FBSDKShareKit 
import CoreLocation 

struct localizacaoActualizada { 

static var rua = "" 
static var localidade = "" 
static var codigopostal = "" 
static var giropostal = "" 
static var country = "" 


} 


class ViewController: UIViewController, ENSideMenuDelegate, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate, CLLocationManagerDelegate { 



var logInViewController: PFLogInViewController! = PFLogInViewController() 
var signUpViewController: PFSignUpViewController! = PFSignUpViewController() 

var localidadeActual = "" 

@IBOutlet weak var userLogedIn: UILabel! 

let locationManager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.sideMenuController()?.sideMenu?.delegate = self 

    self.locationManager.delegate = self 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    self.locationManager.requestWhenInUseAuthorization() 
    self.locationManager.requestAlwaysAuthorization() 
    self.locationManager.startUpdatingLocation() 



    // Se ta loggado 
    var user = PFUser.currentUser() 


    if (user?.username != nil) { 



     // self.performSegueWithIdentifier("aposLogin", sender: self) 


    } 



} 


override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(true) 

    var user = PFUser.currentUser() 
    self.userLogedIn.text = user?.username 




    if (user?.username == nil) { 

     self.logInViewController.fields = (PFLogInFields.UsernameAndPassword | PFLogInFields.LogInButton | PFLogInFields.SignUpButton | PFLogInFields.PasswordForgotten | PFLogInFields.DismissButton) 

     //PFLogInFields.Facebook 




     var logInLogoTitle = UILabel() 
     logInLogoTitle.text = "ParaFora" 

     var SignUpLogoTitle = UILabel() 
     SignUpLogoTitle.text = "ParaFora" 

     //self.logInViewController.facebookPermissions = [ "friends_about_me" ] 
     self.logInViewController.logInView!.logo = logInLogoTitle 
     self.logInViewController.delegate = self 
     self.signUpViewController.signUpView!.logo = SignUpLogoTitle 
     self.signUpViewController.delegate = self 
     self.logInViewController.signUpController = self.signUpViewController 

    } 

    else { 

     //Tentativa 1 
     //self.locationManager.stopUpdatingLocation() 
     var user = PFUser.currentUser() 
     user?["localidade"] = localidadeActual 
     user?.saveInBackground() 



     //tentativa 2 

     //var user = PFUser.currentUser() 
     //var idUser = user?["objectId"] as! String 
     // println("O id do utilizador actual:" + idUser) 

     // var query = PFQuery(className:"User") 
     // query.getObjectInBackgroundWithId(idUser) { 
     //  (User: PFObject?, error: NSError?) -> Void in 
     //  if error != nil { 
     //   println(error) 
     //  } else if let User = User { 
     //   println("Tentativa de escrever" + self.localidadeActual + "no servidor") 
     //   User["localidade"] = self.localidadeActual 
     //   User.saveInBackground() 
    //   println("done?") 
    //  } 
     // } 
     // 



    // self.performSegueWithIdentifier("aposLogin", sender: self) 
    } 
} 

//Localizacao 

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) 
{ 

    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in 

     if (error != nil) 
     { 
      println("Error: " + error.localizedDescription) 
      return 
     } 

     if placemarks.count > 0 
     { 
      let pm = placemarks[0] as! CLPlacemark 
      self.displayLocationInfo(pm) 
     } 
     else 
     { 
      println("Error with the data.") 
     } 
    }) 
} 


func displayLocationInfo(placemark: CLPlacemark) 
{ 
    locationManager.stopUpdatingLocation() 
    //self.locationManager.stopUpdatingLocation() 
    println(placemark.locality) 
    println(placemark.postalCode) 
    println(placemark.administrativeArea) 
    println(placemark.country) 
    println(placemark.location) 
    println(placemark.administrativeArea) 
    println(placemark.subAdministrativeArea) 

//  println(placemark.addressDictionary) 
    //  println(placemark.areasOfInterest) 

    localidadeActual = placemark.locality 
    localizacaoActualizada.rua = placemark.name 
    localizacaoActualizada.localidade = placemark.locality 

    //Guarda o codigo postal na estrutura 
    if count(placemark.postalCode) > 4{ 
    localizacaoActualizada.codigopostal = placemark.postalCode.substringWithRange(Range<String.Index>(start: advance(placemark.postalCode.startIndex, 0), end: advance(placemark.postalCode.endIndex, -3))) 
    localizacaoActualizada.giropostal = placemark.postalCode.substringWithRange(Range<String.Index>(start: advance(placemark.postalCode.startIndex, 4), end: advance(placemark.postalCode.endIndex, 0))) 

    } 

    else { 
     localizacaoActualizada.codigopostal = placemark.postalCode 
    } 





    // Obtem a localidade no serv 


    var user = PFUser.currentUser() 
    println(user?["localidade"]) 
    //fim obter localidade 

} 

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) 
{ 
    println("Error: " + error.localizedDescription) 
} 

//fim local 


// MARK: Parse Login 

func logInViewController(logInController: PFLogInViewController, shouldBeginLogInWithUsername username: String, password: String) -> Bool { 


    if (!username.isEmpty || !password.isEmpty) { 
     return true 
    }else { 
     return false 
    } 

} 



func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser) { 

    self.dismissViewControllerAnimated(true, completion: nil) 

    self.performSegueWithIdentifier("aposLogin", sender: self) 


} 

func logInViewController(logInController: PFLogInViewController, didFailToLogInWithError error: NSError?) { 
    println("Failed to login...") 
} 

func logInViewControllerDidCancelLogIn(logInController: PFLogInViewController) { 

} 

// MARK: Parse Signup 

func signUpViewController(signUpController: PFSignUpViewController, didSignUpUser user: PFUser) { 


    self.dismissViewControllerAnimated(true, completion: nil) 

    self.performSegueWithIdentifier("setupCliente", sender:self) 


} 
func signUpViewController(signUpController: PFSignUpViewController, didFailToSignUpWithError error: NSError?) { 

    println("FAiled to sign up...") 

} 



func signUpViewControllerDidCancelSignUp(signUpController: PFSignUpViewController) { 

    println("User dismissed sign up.") 

} 

// MARK: Actions 

@IBAction func simpleAction(sender: AnyObject) { 

    self.presentViewController(self.logInViewController, animated: true, completion: nil) 

} 



@IBAction func logoutAction(sender: AnyObject) { 

    PFUser.logOut() 

} 



override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


@IBAction func toggleSideMenu(sender: AnyObject) { 
    toggleSideMenuView() 
} 

// MARK: - ENSideMenu Delegate 
func sideMenuWillOpen() { 
    println("sideMenuWillOpen") 
} 

func sideMenuWillClose() { 
    println("sideMenuWillClose") 
} 

func sideMenuShouldOpenSideMenu() -> Bool { 
    println("sideMenuShouldOpenSideMenu") 
    return true 
} 
} 
+0

你檢查了signUpViewController-方法被調用? – Christian

+0

是的,因爲它關閉了viewcontroller並且我使用調試器進行了檢查。 –

回答

0

在你signUpViewController功能你解僱當前視圖控制器,然後你與發件人爲調用performSegueWithIdentifier你剛剛解僱的視圖控制器。嘗試刪除dismissViewControllerAnimated方法調用

+0

已經嘗試過,沒有成功。我有類似的工作:'logInViewController'。但註冊它不工作= / –

0

我有同樣的問題。但我設法解決它,但它不是一個完美的解決方案。我所做的就是解僱PFSignUpViewController

但在此之前,我設置了一個全局變量,在我的情況下稱爲initialSignUp的布爾爲true。一旦控制器被解僱,我檢查了PFLogInViewControllerviewDidAppear()方法,如果initialSignUp爲真。 如果是這種情況,我將它改回false,然後執行segue。

此替代方法適用於我,我希望我可以幫助您解決您的問題。

0

我認爲你需要設置loginViewController的signupViewController的委託。像這樣的東西(抱歉,它在迅速,但你可以propably工作了):

let loginViewController = LoginViewController() 
loginViewController.delegate = self 
loginViewController.signUpController?.delegate = self 

,然後,你可以調用下面的方法來關閉該視圖和執行賽格瑞(或其他任何東西)。

func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser) { 
    self.dismissViewControllerAnimated(true, completion: nil) 
    self.performSegueWithIdentifier("segueID") 

} 
func signUpViewController(signUpController: PFSignUpViewController, didSignUpUser user: PFUser) -> Void { 
    self.dismissViewControllerAnimated(true, completion: nil) 
    self.performSegueWithIdentifier("segueID") 
} 

希望得到這個幫助。它爲我工作。

乾杯, 朱利安