stack amr
stack amr

Reputation: 1

Button.titleLabel error in swift3

I am trying to compare the title that I have in a button so that if it is compared to another it does a certain thing or not. I'm using the .setTitle function but I think in swift3 it gives error and I can not find the why. Then I leave a catch of the error to make it easier to see it and also the code itself. Thanks for the help

enter image description here

@IBAction func _login_button(_ sender: Any) 
{
    if(_login_button.titleLabel?.text == "OKEY")
    {
        let preferences = UserDefaults.standard
        preferences.removeObject(forKey: "session")

        LoginToDo()
        return
    }

    let username = _username.text
    let password = _password.text

    if(username == "" || password == "")
    {
        return
    }

    DoLogin(username!, password!)
}

Upvotes: 0

Views: 127

Answers (4)

Anbu.Karthik
Anbu.Karthik

Reputation: 82779

change your @IBAction func _login_button(_ sender: Any) to you need to type cast from Any to UIButton @IBAction func _login_button(_ sender: UIButton)

@IBAction func _login_button(_ sender: UIButton) 
      {
      if(sender.titleLabel?.text == "OKEY")
        {
    }
}

Upvotes: 3

Pravin Tate
Pravin Tate

Reputation: 1130

If you get this error as per screen shot, you need to type cast from Any to UIButton and then use titleLabel property.

Upvotes: 0

KKRocks
KKRocks

Reputation: 8322

you need to get button as below , now you access function name instead of sender

@IBAction func _login_button(_ sender: Any) 
        let button: UIButton = sender as! UIButton


      if(button.titleLabel?.text == "OKEY")
        {
    }
}

Upvotes: 0

Bhupat Bheda
Bhupat Bheda

Reputation: 1978

_login_button is @IBAction you have to create the button for @IBOutlet of button

Upvotes: 0

Related Questions