UIButton 按钮标题在单击其标题的另一个按钮时会自行更改



我的应用程序中有 12 个按钮。 最后一个按钮标题会自行更改为我按下的任何其他按钮的标题。 例如,如果按钮 3 的标题是 Joe,则最后一个按钮 (tag 12) 会自动更改为 Joe(即使没有代码可以更改其标题。 这太奇怪了!

PS:我最初在我的代码中设置了标题:

func btnSetIcon(btnNumber: Int){
    println("In btnSetIcon btnNumber = (btnNumber)")
    btn.titleLabel?.numberOfLines = 2
    btn.titleLabel?.textAlignment = NSTextAlignment.Center
    btn.titleLabel?.adjustsFontSizeToFitWidth = true
    detail1.keyboardType = UIKeyboardType.ASCIICapable
    detail1.rightViewMode = UITextFieldViewMode.Never
    switch btnNumber {
        case 1: btn.setTitle("BUCKETnLIST", forState: .Normal)
        case 2: btn.setTitle("SHOPnLIST", forState: .Normal)
        case 3: btn.setTitle("GROCERYnLIST", forState: .Normal)
        case 4: btn.setTitle("TO DOnLIST", forState: .Normal)
        case 5: btn.setTitle("NOTESnTO SELF", forState: .Normal)
        case 6: btn.setTitle("SOSnLIST", forState: .Normal)
        case 7: btn.setTitle("PHONEn#'s", forState: .Normal)
        case 8: btn.setTitle("EVENTnLIST", forState: .Normal)
        case 9: btn.setTitle("YOUR 1nLIST", forState: .Normal)
        case 10: btn.setTitle("YOUR 2nLIST", forState: .Normal)
        case 11: btn.setTitle("YOUR 3nLIST", forState: .Normal)
        case 12: btn.setTitle("YOUR 4nLIST", forState: .Normal)
        default: btn.setTitle("✚", forState: .Normal)
    }
}

你没有将变量btn传递到btnSetIcon(),所以它必须是你viewController的属性。 btn可能被保留为您添加到viewController的最后一个按钮,因此当您调用btnSetIcon()时,这是更新的按钮。

也许你想在btnSetIcon()做这样的事情:

if let btn = self.view.viewWithTag(btnNumber) as? UIButton {
    // update btn in here
}

最新更新