简体   繁体   English

用另一个UIButton文本更改UIButton文本(swift)

[英]change UIButton text with another UIButton text (swift)

My question is how to change UIButton 's title with another UIButton 's title. 我的问题是如何用另一个UIButton的头衔来改变UIButton的头衔。 I have three buttons and I want to exchange title for button 1 and 3 with each other when I click on button 2. 我有三个按钮,当我点击按钮2时,我想互相交换按钮1和3的标题。

Look at this image and you will understand. 看看这张图片你会明白的。

how can I do that? 我怎样才能做到这一点?

图片

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. 希望它能帮到你。

Try this 试试这个

let title1 = button1.currentTitle!
button1.setTitle(button3.currentTitle!, forState: .Normal)
button3.setTitle(title1, forState: .Normal)

Try using below snippet, you will change/toggle button1 and button3 title on click of button2.(Whatever title they have) 尝试使用下面的代码片段,您将在点击button2时更改/切换按钮1和按钮3标题。(无论他们拥有什么标题)

@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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM