Reputation: 111
My question is how to change UIButton
's title with another UIButton
's title.
I have three buttons and I want to exchange title for button 1 and 3 with each other when I click on button 2.
Look at this image and you will understand.
how can I do that?
Upvotes: 3
Views: 1440
Reputation: 1441
Try using below snippet, you will change/toggle button1 and button3 title on click of button2.(Whatever title they have)
@IBAction func btn2Tapped(btn2 : UIButton)
{
var strBtnTittle : String!
if (btn2.selected)
{
btn2.selected=false
strBtnTittle = btn1.titleLabel?.text
btn1.setTitle(btn3.titleLabel?.text, forState: .Normal)
btn3.setTitle(strBtnTittle, forState: .Normal)
}
else
{
btn2.selected=true
strBtnTittle = btn3.titleLabel?.text
btn3.setTitle(btn1.titleLabel?.text, forState: .Normal)
btn1.setTitle(strBtnTittle, forState: .Normal)
}
}
Upvotes: 1
Reputation: 23882
I think it's very simple.
Just preserve one of the text from button in a String and replace it by clicking on a button.
let title1 = btn1.titleLabel!.text
btn1.setTitle(btn2.titleLabel!.text, forState: UIControlState.Normal)
btn2.setTitle(title1, forState: UIControlState.Normal)
Learn and apply the code! Hope it helps you.
Upvotes: 3
Reputation: 1396
Try this
let title1 = button1.currentTitle!
button1.setTitle(button3.currentTitle!, forState: .Normal)
button3.setTitle(title1, forState: .Normal)
Upvotes: 1