Swift 如何更改按钮´;通过单击另一个按钮来设置文本?-敏捷的

Swift 如何更改按钮´;通过单击另一个按钮来设置文本?-敏捷的,swift,uibutton,Swift,Uibutton,基本上这就是我所尝试的: @IBAction func buttonTwo(sender: UIButton){ // <- Should UIButton be AnyObject ? buttonOne.setTitle("Do", forState: .Normal) // This wont change the other buttons text } 我想这就是你想要的: @IBAction func buttonTwo(sender: UIButton) {

基本上这就是我所尝试的:

@IBAction func buttonTwo(sender: UIButton){  // <- Should UIButton be AnyObject ? 
 buttonOne.setTitle("Do", forState: .Normal) // This wont change the other buttons text 
}

我想这就是你想要的:

@IBAction func buttonTwo(sender: UIButton) {
    if let text = buttonOne.titleForState(UIControlState.Normal) { //make sure title is not nil
        if ( text == "What") {
            buttonOne.setTitle("Do", forState: .Normal)
        } else {
            buttonOne.setTitle("What", forState: .Normal)
        }  
    }
}
我只是跑得很好:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var buttonOne: UIButton!

    @IBAction func changeText(sender: AnyObject) {

        if let text = buttonOne.titleForState(UIControlState.Normal) {
            if ( text == "What") {
                buttonOne.setTitle("Do", forState: .Normal)
            } else {
                buttonOne.setTitle("What", forState: .Normal)
            }  
        }

    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
您确定buttonOne的@IBOutlet连接正确吗

注意:当从故事板创建@iAction时,它将发送者设置为AnyObject。我改变了它,看看它是否像UIButton一样工作,它确实如此

如您所问的数组选项

class ViewController: UIViewController {

@IBOutlet weak var buttonOne: UIButton!

var buttonArray:[UIButton] = []

@IBAction func changeText(sender: UIButton) {
    if let text = buttonArray[0].titleForState(UIControlState.Normal) {
        if ( text == "What") {
            buttonArray[0].setTitle("Do", forState: .Normal)
        } else {
            buttonArray[0].setTitle("What", forState: .Normal)
        }  
    }

}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    buttonArray.append(buttonOne)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

如果语句缺少括号,
setTile
显然是一个打字错误,您正在尝试使用
===
ui按钮
字符串
进行比较。我知道这一点。。。我只是想说明我的逻辑,我在这里打字很快。即使使用正确的命令,它也不会像我想要的那样工作。。。我改正了,帮我们,帮你。如果您甚至不愿意通过编译器运行代码,我们很难帮助您。我确信,如果您确实尝试在Xcode中运行此命令,您将能够修复编译器错误并查看错误所在。我确实在编译器上运行了我的代码,我的主要问题是通过单击与上述代码不兼容的其他按钮来更改另一个按钮文本(buttonOne.setTitle(“Do”,forState:.Normal),我已尝试为此寻找解决方案。我已尝试在我的帖子中明确说明。很抱歉遇到任何麻烦。我运行了此程序,但它显示了一个错误,即:`(UIButton)->0'没有名为` titleLabel'的成员。我不确定这是正确的命令使用,当我键入'buttonOne.setTitle(“Do,forState:.Normal),它不会尝试为setTitle自动完成,也不会将setTitle和.Normal转换为不同的颜色。我修改了if-let行。尝试相同的方法,但使用titleForState。您确定您的按钮以标题集开始吗?是的,它确实有标题。
class ViewController: UIViewController {

@IBOutlet weak var buttonOne: UIButton!

var buttonArray:[UIButton] = []

@IBAction func changeText(sender: UIButton) {
    if let text = buttonArray[0].titleForState(UIControlState.Normal) {
        if ( text == "What") {
            buttonArray[0].setTitle("Do", forState: .Normal)
        } else {
            buttonArray[0].setTitle("What", forState: .Normal)
        }  
    }

}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    buttonArray.append(buttonOne)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}