-1

I am just learning the swift basics and thought it would be a good idea that I try using my skills and a problem appeared. I have tried everything I know can someone help. My image below.

enter image description here

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Maddog
  • 11
  • 1
  • 2
  • `UIButton` that's the class, not the object. You meant `number.title` Also, copy/paste code not screenshot. But that's not how you update the title for a UIButton, use setTitle:forState – Larme Jul 10 '18 at 09:48
  • see this example : https://stackoverflow.com/questions/1033763/is-it-possible-to-update-uibutton-title-text-programmatically – Anbu.Karthik Jul 10 '18 at 09:48
  • First of all, I would recommend that you should add the code as a snippet instead of screenshot. – Ahmad F Jul 10 '18 at 09:51
  • @Larme I will paste my code but I did that it came up with the same problem. – Maddog Jul 10 '18 at 09:52
  • @vadian Thanks. – Maddog Jul 10 '18 at 10:06

5 Answers5

4

First of all please post text, not images

You have to use the (IBOutlet) instance number rather than the type UIButton and you have to use the proper API

number.setTitle(String(score), for: .normal)

But in an IBAction I'd declare the method with the static sender type (rather than unspecified Any) and use that

@IBAction func touched(_ sender : UIButton) {
    score += 1
    sender.setTitle(String(score), for: .normal)
}
vadian
  • 274,689
  • 30
  • 353
  • 361
0

If you want to change the title of a button, you need to do this:

  • button_Outlet_Name.setTitle(title: String?, for: UIControlState) or
  • button_Outlet_Name.title.text = "New Title"

Remember to do this on your button OUTLET, not on the UIButton class

Linh Ta
  • 593
  • 6
  • 11
0

Here you are accessing the static method of the UIButton class. If you want to set the title, you need to do so on the instance. Based on what you have, within the IBAction, you can cast the sender as a UIButton and then set the title. You’d also do that for a specific state since the titles are closely related to the the state for a UIButton.

if let btn = sender as? UIButton {
    btn.setTitle(“\(score)”, forState: .normal)
}

You could have also used the IBOutlet reference instead.

number.setTitle(“\(score)”, forState: .normal)

Whenever you use \(variable) within a string, it uses the string value of the variable to be displayed in the string.

Khanal
  • 788
  • 6
  • 14
0

You cannot change the your button's title that way, what you have done there by writing

UIButton.title = String(score)

This means you are calling a static method of UIButton class and the name of the method is title.

If you want to change the button's tite you can do that the below way:

Step 1: Take a reference of your button by ctrl+drag.

Step 2: Inside your IBAction you need to write:

yourButton.setTitle("\(score)", for: .normal)
iPeter
  • 1,330
  • 10
  • 26
0

I'm not sure what are u trying to do but if you want to change the title of the clicked button u can do like this:

@IBAction func touched(_ sender: Any) {
    score += 1
    // check if the sender is UIButton
    if let button = sender as? UIButton {
        //change your button title
        button.setTitle("\(scroe)", for: UIControlState.normal)
    }
}
Ayoub Nouri
  • 201
  • 1
  • 7