0

I have tried to implement the solution from this answer but I just haven't been able to get it to work. I get 'Use of unresolved identifier...' for the variables I am trying to access..

I have a loginViewController.swift that gets the username and password when someone logs in..

import UIKit

class loginViewController: UIViewController {

@IBOutlet weak var userEmailTextField: UITextField!
@IBOutlet weak var userPasswordTextField: UITextField!


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

@IBAction func loginButtonTapped(sender: AnyObject) {

    let userEmail = userEmailTextField.text;
    let userPassword = userPasswordTextField.text;

Then I have a PostService.swift file that gets data from a MySQL database.. It should use the username of the person logged in to access the relevant data..

import Foundation

class PostService {

var settings:Settings!

let userEmail = "steve@centrix.co.za"; //This is hard coded but needs to come from the loginViewController
let userPassword = "1234";             //This is hard coded but needs to come from the loginViewController

I have tried that and a few other suggestions and I just don't seem to be able to get it to work.. Please help..

Thanks in advance.

Community
  • 1
  • 1
Steve Rowe
  • 237
  • 1
  • 3
  • 11
  • Can you please share actual code you are working with? How do you instantiate PostService class? You need to declare variables globally so and you can use self keyword to access them. – Seyhun Akyürek Aug 08 '15 at 17:03

1 Answers1

2

You can just declare

var userEmail = "";
var userPassword = "";

outside a class (it doesn't matter which Swift class). Then, dropping the let,

@IBAction func loginButtonTapped(sender: AnyObject) {
    userEmail = userEmailTextField.text;
    userPassword = userPasswordTextField.text;

will store the variables, and you'll just be able to use the in the PostService class.

However, is there really no connection between the LoginViewController and the PostService? You'll have to call a PostService function somewhere ... maybe you can pass the variables there?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • Thanks Glorfindel. I struggled with this for a few days... The variables stayed empty. I then realised that I needed to click the login button to pass the values to the variables... Reset my iOS Simulator, logged in and bam! – Steve Rowe Aug 15 '15 at 08:42