Craig.Pearce
Craig.Pearce

Reputation: 746

Accessing the text in TextField by Cosmicmind

I am having some trouble using the text in textField. I've copied the relevant code below (and removed some things here and there that shouldn't affect the errors I am getting).

Am I missing something here? I feel like this should be easy but I can't seem to figure it out.

class loginPageViewController: UIViewController, TextFieldDelegate {

private var email: TextField!
private var password: TextField!

func loginButton(sender: RaisedButton!){
    let parameters = [
        "email" : email,
        "password" : password
    ]  

    Alamofire.request(.POST, "https://kidgenius.daycareiq.com/api/v1/sessions", parameters: parameters, encoding: .URL)
    //other alamofire code not relevant to question
}

override func viewDidLoad() {
    super.viewDidLoad()

    prepareEmail()
    preparePassword()
    prepareLoginButton()
}  

private func prepareEmail() {        
    let email : TextField = TextField(frame: 100, 100, 200, 45))
    email.delegate = self
    email.placeholder = "Email"
    //I removed some non relevant code here, just styling stuff
    view.addSubview(email)
}

private func preparePassword() {
    let password : TextField = TextField(100, 200, 200, 25))
    password.secureTextEntry = true
    password.delegate = self
    password.placeholder = "Password"
    //some more removed styling code
    view.addSubview(password)
    }

private func prepareLoginButton() {
    let loginButton : RaisedButton = RaisedButton(frame: 100, 250, 150 , 35))
    loginButton.setTitle("Login", forState: .Normal)
    loginButton.addTarget(self, action: "loginButton:", forControlEvents: UIControlEvents.TouchUpInside)
    //removed some styling code here
    view.addSubview(loginButton)
    }
}

Upvotes: 0

Views: 201

Answers (1)

Breek
Breek

Reputation: 1441

The issue is you declare your email variable on the top

private var email: TextField!

And then you also declare the email inside the function prepareEmail() again

let email : TextField = TextField(frame: 100, 100, 200, 45))

But your private var email: TextField! never gets instantiated

In order to fix your issue, remove the let when you instantiate the variable inside your prepareEmail()

Upvotes: 1

Related Questions