使用变量在swift中设置按钮的标题

时间:2015-07-05 07:59:37

标签: ios swift

我正在尝试根据随机选择动态更改按钮的标题。 我收到了错误;

'String' does not have a member named 'setTitle'

我认为这是因为变量未被识别为'按钮'我试图更新标题,我只是不知道如何解决它。 我已经尝试制作UIButton类型的数组,但这似乎没有区别

    var choicesArray = Array(["choiceOne", "choiceTwo", "choiceThree", "choiceFour"])
var randomChoice = Int(arc4random_uniform(5))

@IBOutlet weak var choiceOne: UIButton!
@IBOutlet weak var choiceTwo: UIButton!
@IBOutlet weak var choiceThree: UIButton!
@IBOutlet weak var choiceFour: UIButton!

func setLabels() {
    var timesTable = toPass.toInt()
    var answer = timesTable! * multiplier
    var randomChoiceTitle = choicesArray[randomChoice]
    randomChoiceTitle.setTitle(String(answer), forState: UIControlState.Normal)

正在生成错误;

randomChoiceTitle.setTitle

4 个答案:

答案 0 :(得分:0)

var choicesArray: [UIButton]! {
    get {
        return [choiceOne, choiceTwo, choiceThree, choiceFour]
    }
}

答案 1 :(得分:0)

数组choicesArray必须包含UIButton引用而不是文字字符串

@IBOutlet weak var choiceOne: UIButton!
@IBOutlet weak var choiceTwo: UIButton!
@IBOutlet weak var choiceThree: UIButton!
@IBOutlet weak var choiceFour: UIButton!

let choicesArray = [choiceOne, choiceTwo, choiceThree, choiceFour]
var randomChoice = Int(arc4random_uniform(5))

func setLabels() {
    var timesTable = toPass.toInt()
    var answer = timesTable! * multiplier
    let randomButton = choicesArray[randomChoice]
    randomButton.setTitle(String(answer), forState: .Normal)

答案 2 :(得分:0)

choiceArray应该有对该按钮的引用,然后你可以改变按钮的标题。将选择数组更改为包含按钮的数组。

@IBOutlet weak var choiceOne: UIButton!
@IBOutlet weak var choiceTwo: UIButton!
@IBOutlet weak var choiceThree: UIButton!
@IBOutlet weak var choiceFour: UIButton!

//choicearray containing the buttons 1-4
let choicesArray = [choiceOne, choiceTwo, choiceThree, choiceFour]
var randomChoice = Int(arc4random_uniform(5)) 

func setLabels() {
    var timesTable = toPass.toInt()
    var answer = timesTable! * multiplier
    var randomChoiceTitle = choicesArray[randomChoice]
    randomChoiceTitle.setTitle(String(answer), forState: UIControlState.Normal)
}

答案 3 :(得分:0)

这是您完整的工作代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var choiceOne: UIButton!
    @IBOutlet weak var choiceTwo: UIButton!
    @IBOutlet weak var choiceThree: UIButton!
    @IBOutlet weak var choiceFour: UIButton!

    var choicesArray = [UIButton]()  //Create a button type array
    let toPass = "1"
    let multiplier = 0
    let randomChoice = Int(arc4random_uniform(4))  //Change this to 4 because you array have 0 to 3 elements

    override func viewDidLoad() {
        super.viewDidLoad()
        println(randomChoice)
        choicesArray = [choiceOne, choiceTwo, choiceThree, choiceFour]   //add buttons into your array.
        setLabels()

    }

    func setLabels() {
        var timesTable = toPass.toInt()
        var answer = timesTable! * multiplier
        var randomChoiceTitle = choicesArray[randomChoice]
        randomChoiceTitle.setTitle(String(answer), forState: UIControlState.Normal)
    }
}