将标题设置为按钮并在单击两次时再次返回第一个标题
SetTitle to a button and back to the first title again when clicked twice
我设法通过单击更改按钮本身的标题和标签的文本。
最重要的是,我希望同一个按钮的标题和标签中的相同文本在第二次单击时再次更改。考虑到我已经构建的代码,我该如何添加它?
@IBOutlet weak var changeDegreeDisplay: UILabel!
@IBOutlet weak var radPressed: UIButton!
@IBAction func radianPressed(sender: AnyObject) {
radPressed.setTitle("Deg", forState: .Normal)
changeDegreeDisplay.text = "radians"
}
您可能希望使用 if 语句来检查按钮的当前标题或其状态,具体取决于您的用例。
@IBAction func radianPressed(sender: UIButton) {
if sender.currentTitle == "Deg" {
sender.setTitle("Rad", forState: .Normal)
} else {
sender.setTitle("Deg", forState: .Normal)
}
}
抓住按钮的标题,检查它是 "Deg" 还是 "Rad",然后将其切换到正确的标题。
@IBOutlet weak var changeDegreeDisplay: UILabel!
@IBOutlet weak var radPressed: UIButton!
@IBAction func radianPressed(sender: AnyObject) {
let title = radPressed.titleForState(.Normal)
if title=="Deg" {
radPressed.setTitle("Rad", .Normal)
changeDegreeDisplay.text = "degrees"
} else {
radPressed.setTitle("Deg", .Normal)
changeDegreeDisplay.text = "radians"
}
}
我建议将标题存储为常量,因此 let degTitle = "Deg"
和 let radTitle = "Rad"
,然后在条件语句中使用这些常量进行比较。
我设法通过单击更改按钮本身的标题和标签的文本。 最重要的是,我希望同一个按钮的标题和标签中的相同文本在第二次单击时再次更改。考虑到我已经构建的代码,我该如何添加它?
@IBOutlet weak var changeDegreeDisplay: UILabel!
@IBOutlet weak var radPressed: UIButton!
@IBAction func radianPressed(sender: AnyObject) {
radPressed.setTitle("Deg", forState: .Normal)
changeDegreeDisplay.text = "radians"
}
您可能希望使用 if 语句来检查按钮的当前标题或其状态,具体取决于您的用例。
@IBAction func radianPressed(sender: UIButton) {
if sender.currentTitle == "Deg" {
sender.setTitle("Rad", forState: .Normal)
} else {
sender.setTitle("Deg", forState: .Normal)
}
}
抓住按钮的标题,检查它是 "Deg" 还是 "Rad",然后将其切换到正确的标题。
@IBOutlet weak var changeDegreeDisplay: UILabel!
@IBOutlet weak var radPressed: UIButton!
@IBAction func radianPressed(sender: AnyObject) {
let title = radPressed.titleForState(.Normal)
if title=="Deg" {
radPressed.setTitle("Rad", .Normal)
changeDegreeDisplay.text = "degrees"
} else {
radPressed.setTitle("Deg", .Normal)
changeDegreeDisplay.text = "radians"
}
}
我建议将标题存储为常量,因此 let degTitle = "Deg"
和 let radTitle = "Rad"
,然后在条件语句中使用这些常量进行比较。