lundi 27 juin 2016

Get user email from loginviewcontroller and show in revealview's label

I have almost built an app but I want to show the email address of current user in the reveal view or side view's label. The reveal view is a tableviewcontroller and the loginview is a simple viewcontroller.

I used following code in login view controller:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if let target = segue.destinationViewController as? backTableVC {
           target.labelname = NSUserDefaults.standardUserDefaults().stringForKey("currentuser")
        }
    }

And the following code in the reveal view controller:

var labelname:String? {
    didSet {
    emailLabel!.text = labelname
    }
}
 @IBOutlet weak var emailLabel: UILabel! {
    didSet {
        emailLabel.text = labelname
    }
}

override func viewDidLoad() {
super.viewDidLoad()

emailLabel.text = labelname     
}

I put the breakpoint at the last line of this code but I get nil as lablename's value and it also does not change value in the label of reveal view's label.

The code for login view controller is as

import UIKit

class LoginViewController: UIViewController,UITextFieldDelegate {

@IBOutlet weak var userPasswordTextField: UITextField!
@IBOutlet weak var userEmailTextField: UITextField!
var currentuseremail:String?
override func viewDidLoad() {
    super.viewDidLoad()
    self.userEmailTextField.delegate = self
    self.userPasswordTextField.delegate = self

    // Do any additional setup after loading the view.
}
func textFieldShouldReturn(userText: UITextField) -> Bool {
    userText.resignFirstResponder()
    return true;
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
var userEmail:String?
var userPassword:String?
@IBAction func loginButton(sender: AnyObject) {
    if (userEmailTextField.text! != "")
    {
        userEmail = userEmailTextField.text}
    else {
        alert("Please Enter Your email")
    }
    if (userPasswordTextField != ""){
        userPassword = userPasswordTextField.text}
    else {
        alert("Please Enter Password")
    }
    let userEmailStored = NSUserDefaults.standardUserDefaults().stringForKey("userEmail")
    let userPasswordStored = NSUserDefaults.standardUserDefaults().stringForKey("userPassword")

    if(userEmailStored == userEmail) {
        if(userPasswordStored == userPassword) {
            // Login is Successfull
            currentuseremail = userEmail
            NSUserDefaults.standardUserDefaults().setObject(currentuseremail, forKey: "currentuser")
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIN")

            NSUserDefaults.standardUserDefaults().synchronize()
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }
    else {
        let myAlert = UIAlertController(title: "Alert", message: "Email or Password doesn't match", preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
        myAlert.addAction(okAction)
        self.presentViewController(myAlert, animated: true, completion: nil)

    }

}
func alert(message:String) {
    let myalert = UIAlertController(title: "Alert", message: message, preferredStyle: .Alert)
    let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
    myalert.addAction(action)
    self.presentViewController(myalert, animated: true, completion: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let target = segue.destinationViewController as? backTableVC {
       target.labelname = NSUserDefaults.standardUserDefaults().stringForKey("currentuser")
    }
}

}

Aucun commentaire:

Enregistrer un commentaire