0
votes

What I am trying to do here is once login is successful, go to the "HomeLoginViewController" which is a navigation controller with its root controller being "HomepageViewController" but I also need to send the surname of the user that logged in to a label in the "HomepageViewController".

If I put the "self.presentViewController(controller, animated: true, completion: nil)" first then this goes to the "HomepageViewController" and sends the surname but does not display the navigation bar.

If I put the "self.presentViewController(alert, animated: true, completion: nil)" first it displays the navigation bar but does not send the surname. I cannot fix this code to do both :( Please help.

class LoginViewController: UIViewController {
let ref = Firebase(url: "https://<myfirebase>.firebaseio.com/")


@IBOutlet weak var forenamefield: UITextField!

@IBOutlet weak var surnamefield: UITextField!
@IBOutlet weak var emailfield: UITextField!
@IBOutlet weak var passwordfield: UITextField!
@IBOutlet weak var loginbutton: UIButton!



@IBAction func signinaction(sender: AnyObject) {
    let user = self.forenamefield.text!
    let usersurname = self.surnamefield.text!


    ref.authUser(emailfield.text, password: passwordfield.text, withCompletionBlock: { error, authData in

        if error != nil
        {
            let alert = UIAlertController(title: "Error", message: "Incorrect Email and Room Number.", preferredStyle: UIAlertControllerStyle.Alert)
            let action = UIAlertAction(title: "Ok", style: .Default, handler: nil)
            alert.addAction(action)
            self.presentViewController(alert, animated: true, completion: nil)
            print("Can Not Sign In")
        }
        else
        {
            let storyboard = UIStoryboard(name: "Main", bundle: nil);
            let viewName:NSString = "HomeView"
            let vc = storyboard.instantiateViewControllerWithIdentifier(viewName as String) as! HomeLoginViewController

            let controller = storyboard.instantiateViewControllerWithIdentifier("ViewHome") as! HomepageViewController

            controller.labelText = self.surnamefield.text!


            let uid = authData.uid
            print("Success with user: \(uid)")

            let alert = UIAlertController(title: "Success", message: "Welcome \(user) \(usersurname)", preferredStyle: UIAlertControllerStyle.Alert)
            let action = UIAlertAction(title: "Ok", style: .Default) { _ in
                self.navigationController?.presentViewController(vc, animated: true, completion: nil)
            }
            alert.addAction(action)

            self.presentViewController(controller, animated: true, completion: nil)
            self.presentViewController(alert, animated: true, completion: nil)



        }

    })
}
1
You can use NSUserDefaults to save the data to your app sandbox. Or you can use Simpleton. Simpleton is recommended if you have many more data to share btw VCs. (Read here about Simpleton: xcodenoobies.blogspot.my/2012/08/how-to-pass-data-between.html) - GeneCode

1 Answers

1
votes

You can try use NSUserDefaults when login details are correct

// In login screen before go to home, save the username as key "userID"
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(username, forKey: "userID")
defaults.synchronize()

// In your home, viewDidLoad() retrieve it back
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
let userID = (defaults.objectForKey("userID") as? String)!