Kleinfeltersville
Kleinfeltersville

Reputation: 117

How to change UIButton label programmatically

When I first run my app, I retrieve a number from my server and display it for my UIButton label. Think of this as a notification number displayed on a red UIButton.

When I remove a notification within the app, I want my UIButton label decrement by 1. I am able to get the decremented number from the server after I delete a notification, but I can't display this new number on the UIButton. The button always displays the number when the app is first fired.

I call makeButtonView() method after I remove a notification to update the UIButton

func makeButtonView(){
    var button = makeButton()
    view.addSubView(button)

    button.tag = 2
    if (view.viewWithTag(2) != nil) {
        view.viewWithTag(2)?.removeFromSuperview()
        var updatedButton = makeButton()
        view.addSubview(updatedButton)
    }else{
        println("No button found with tag 2")
    }


}

func makeButton() -> UIButton{
 let button = UIButton(frame: CGRectMake(50, 5, 60, 40))
 button.setBackgroundImage(UIImage(named: "redBubbleButton"), forState: .Normal)
    API.getNotificationCount(userID) {
        data, error in

        button.setTitle("\(data)", forState: UIControlState.Normal)

    }
    button.addTarget(self, action: "targetController:", forControlEvents: UIControlEvents.TouchUpInside)

    return button

}

Upvotes: 3

Views: 5907

Answers (6)

Shammi Theodore
Shammi Theodore

Reputation: 1

After setting the title, just a simple redraw of the button will do:

button.setNeedsDisplay();

Upvotes: 0

zfetters
zfetters

Reputation: 457

Since your API call should be running on a background thread you need to dispatch your UI update back to the main thread like this:

DispatchQueue.main.async {
      button.setTitle(“new value”, forState: .normal)
  }

Upvotes: 0

AtulParmar
AtulParmar

Reputation: 4570

Use this code for Swift 4 or 5

button.setTitle("Click Me", for: .normal)

Upvotes: 6

Eugene
Eugene

Reputation: 11

There have been some updates since Swift 4. This works for me:

self.button.setTitle("Button Title", for: UIControl.State.init(rawValue: 0))

Replace button with your IBOutlet name. You can also use a variable or array in place of the quoted text.

Upvotes: 1

agy
agy

Reputation: 2854

I need more information to give you a proper code. But this approach should work:

lazy var button : UIButton = {
    let button = UIButton(frame: CGRectMake(50, 5, 60, 40))
    button.setBackgroundImage(UIImage(named: "redBubbleButton"), forState: .Normal)
    button.addTarget(self, action: "targetController:", forControlEvents: UIControlEvents.TouchUpInside)

    return button
    }()

func makeButtonView(){
    // This should be called just once!!
    // Likely you should call this method from viewDidLoad()
    self.view.addSubview(button)
}

func updateButton(){
    API.getNotificationCount(userID) {
        data, error in
        // be sure this is call in the main thread!!
        button.setTitle("\(data)", forState: UIControlState.Normal)
    }
}

Upvotes: 2

ABC
ABC

Reputation: 41

It's fairly simple ...

import UIKit

class ViewController: UIViewController {

    @IBOutlet var button: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        button.setTitle("hello world", forState: UIControlState.Normal)
    }
}

I believe if you set the state to normal, the value will propagate by default to other states so long as you haven't explicitly set a title for those states.

Said differently, if you set it for normal, it should also display this title when the button enters additional states

UIControlState.allZeros
UIControlState.Application
UIControlState.Disabled
UIControlState.Highlighted
UIControlState.Reserved
UIControlState.Selected

Lastly, here's Apple's documentation in case you have other questions.

Upvotes: 0

Related Questions