Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton setTitle doesn't work

I'm trying to change the text of a UIButton when it gets clicked. I've tried the following things, which failed:

First I tried inserting a button on the storyboard and linking it to the view controller, with the code for the button appearing like this:

@IBAction func button(sender: AnyObject) {}. 

Within the button's parentheses, I then used

button.setTitle ("something" , for State: .Selected)

However, when I run this code in the simulator, it doesn't seem to work at all.

I have also tried, following the Apple reference manual, to set different labels for different states in the side menu after clicking on said button, but still this didn't work. Can anyone tell me exactly (i.e. what code to write and where exactly it goes) how to make this happen?

like image 905
JustABeginner Avatar asked Feb 18 '15 18:02

JustABeginner


6 Answers

The type of the sender should be UIButton, when creating the IBAction function. The sender is your button. The function is called when you tap on the button. You must use the function setTitle on the sender(your button).

@IBAction func buttonTouchedUpInside(sender: UIButton) { 
    sender.setTitle("buttonName", forState: .normal)
}
like image 175
gasho Avatar answered Oct 19 '22 20:10

gasho


Swift 4:

Plain Title

The setTitle() method will work for titles that are "Plain" as defined in the button's Attributes inspector.

@IBAction func button(sender: UIButton) { 
     sender.setTitle("buttonName", for: .normal)
}

Attributed Title

The setTitle() method has no effect on a button's title if it's configured as "Attributed" in the Attributes inspector. To manage this situation, first get the attributed title from the button, then set the value.

@IBAction func button(sender: UIButton) { 
     let attributedTitle = sender.attributedTitle(for: .normal)
     attributedTitle?.setValue("buttonName", forKey: "string")
     sender.setAttributedTitle(attributedTitle, for: .normal)
}
like image 32
Dale Avatar answered Oct 19 '22 19:10

Dale


In practically, if you use button from Storyboard or Xib file, and not clear button value in (Storyboard, Xib), than setTitle method doesn't work. If you cleared button value, setTitle method is work.

like image 3
BlowJohan Avatar answered Oct 19 '22 19:10

BlowJohan


Create an IBOutlet for your button, then this code should work (assuming you named your IBOutlet button):

button.setTitle("title", forState: .Normal)
button.setTitle("title 1", forState: .Application)
button.setTitle("title 2", forState: .Highlighted)
button.setTitle("title 3", forState: .Reserved)
button.setTitle("title 4", forState: .Selected)
button.setTitle("title 5", forState: .Disabled)

place it in your viewDidLoad() method.

like image 2
iCode Avatar answered Oct 19 '22 21:10

iCode


This worked for me.

dispatch_async(dispatch_get_main_queue(), ^{
        [self.btnAssment setTitle:@"ASSDate" forState:UIControlStateNormal];
    });
like image 2
Karuppasamy Pandian Avatar answered Oct 19 '22 19:10

Karuppasamy Pandian


let button = UIButton()
button.setTitle("Button", for: .normal)
button.setTitleColor(.black, for: .normal)
like image 2
developer1996 Avatar answered Oct 19 '22 21:10

developer1996