如何将多个操作连接到一个按钮 Xcode?
How to connect multiple action to one button Xcode?
我是 ios 开发的菜鸟,我有一个简单的问题,但我仍然无法解决。事情是我制作了一个反向单词应用程序,当用户第一次点击按钮时它会反转示例文本但是当用户第二次点击相同的按钮时它会清除示例文本和结果标签中的文本。所以主要问题是如何将“清除操作”连接到同一个按钮?
@IBOutlet var actReverseStyle: UIButton!
@IBOutlet weak var sampletext: UITextField!
var sample: String {return sampletext.text ?? ""
}
@IBOutlet weak var resultscreen: UILabel!
@IBAction func actreverse(_ sender: UIButton!) {
let sampleSentence = sample
func reverseWolrdsInSentance(sentanse:String) -> String{
let allWords = sampleSentence.components(separatedBy: " ")
var newSentence = ""
for word in allWords{
if newSentence != ""{
newSentence += " " }
let reverseWord = String(word.reversed())
newSentence += reverseWord}
return newSentence}
resultscreen.text = reverseWolrdsInSentance(sentanse: sampleSentence)
actReverseStyle.setTitle("Clear", for: .normal)
}
}
只需使用一个布尔标志,它会在每次触发动作时切换
private var clearAction = false
@IBAction func actreverse(_ sender: UIButton!) {
if clearAction {
// do clear stuff
clearAction = false
} else {
// do reversing stuff
clearAction = true
}
}
这样可能更方便。
@IBAction func actreverse(_ sender: UIButton!) {
sender.isSelected.toggle();
if sender.isSelected {
// do reverse
} else {
// do clear
}
}
我是 ios 开发的菜鸟,我有一个简单的问题,但我仍然无法解决。事情是我制作了一个反向单词应用程序,当用户第一次点击按钮时它会反转示例文本但是当用户第二次点击相同的按钮时它会清除示例文本和结果标签中的文本。所以主要问题是如何将“清除操作”连接到同一个按钮?
@IBOutlet var actReverseStyle: UIButton!
@IBOutlet weak var sampletext: UITextField!
var sample: String {return sampletext.text ?? ""
}
@IBOutlet weak var resultscreen: UILabel!
@IBAction func actreverse(_ sender: UIButton!) {
let sampleSentence = sample
func reverseWolrdsInSentance(sentanse:String) -> String{
let allWords = sampleSentence.components(separatedBy: " ")
var newSentence = ""
for word in allWords{
if newSentence != ""{
newSentence += " " }
let reverseWord = String(word.reversed())
newSentence += reverseWord}
return newSentence}
resultscreen.text = reverseWolrdsInSentance(sentanse: sampleSentence)
actReverseStyle.setTitle("Clear", for: .normal)
}
}
只需使用一个布尔标志,它会在每次触发动作时切换
private var clearAction = false
@IBAction func actreverse(_ sender: UIButton!) {
if clearAction {
// do clear stuff
clearAction = false
} else {
// do reversing stuff
clearAction = true
}
}
这样可能更方便。
@IBAction func actreverse(_ sender: UIButton!) {
sender.isSelected.toggle();
if sender.isSelected {
// do reverse
} else {
// do clear
}
}