将 Button 标签名称取为 UILongPressGestureRecognizer,将 var 和 return 值更改为 Button 的标签
Take Button label name to UILongPressGestureRecognizer, change var and return value to Button's label
请帮帮我。我想做什么:
我有一些变量和按钮。当我长按按钮时,我想要带有文本字段的警报弹出窗口。然后我想输入一些值 (Int),然后将按钮的标签更改为 (textField value + previous)。我想为多个按钮编写一个函数。我尝试做的事情:
func addLongPressGesture(by sender: UIButton){
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
longPress.minimumPressDuration = 1
sender.addGestureRecognizer(longPress)
}
@objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
print("Long Press")
var textField = UITextField()
let alert = UIAlertController(title: "How much?", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Add", style: .default) { (action) in
if let howMuch = textField.text {
self.VARIABLE1 = self.VARIABBLE1 + Int(howMuch)!
self.viewWillAppear(true)
}
}
alert.addAction(action)
alert.addTextField { (alertTextField) in
textField = alertTextField
textField.placeholder = "How much?"
}
present(alert, animated: true, completion: nil)
}
}
我希望 VARIABLE1 根据我长按的按钮进行更改
听起来您声明了多个变量,每个按钮一个。这是您应该开始使用数组的好兆头。声明一个数组而不是多个变量:
var totals = Array(repeating: 0, count: <however many buttons you have>)
然后,给每个按钮一个不同的 tag
。您也可以在情节提要中设置它。第一个按钮的标签为 0,第二个按钮的标签为 1,依此类推。
在longPress
中获取用gesture.view
点击的按钮,获取标签。标签是您应该递增的数组的索引:
if let howMuch = textField.text {
self.totals[gesture.view!.tag] += Int(howMuch)! // Note: you should probably unwrap the Int? safely
self.viewWillAppear(true) // Note: you shouldn't really call this yourself
}
请帮帮我。我想做什么: 我有一些变量和按钮。当我长按按钮时,我想要带有文本字段的警报弹出窗口。然后我想输入一些值 (Int),然后将按钮的标签更改为 (textField value + previous)。我想为多个按钮编写一个函数。我尝试做的事情:
func addLongPressGesture(by sender: UIButton){
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
longPress.minimumPressDuration = 1
sender.addGestureRecognizer(longPress)
}
@objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
print("Long Press")
var textField = UITextField()
let alert = UIAlertController(title: "How much?", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Add", style: .default) { (action) in
if let howMuch = textField.text {
self.VARIABLE1 = self.VARIABBLE1 + Int(howMuch)!
self.viewWillAppear(true)
}
}
alert.addAction(action)
alert.addTextField { (alertTextField) in
textField = alertTextField
textField.placeholder = "How much?"
}
present(alert, animated: true, completion: nil)
}
}
我希望 VARIABLE1 根据我长按的按钮进行更改
听起来您声明了多个变量,每个按钮一个。这是您应该开始使用数组的好兆头。声明一个数组而不是多个变量:
var totals = Array(repeating: 0, count: <however many buttons you have>)
然后,给每个按钮一个不同的 tag
。您也可以在情节提要中设置它。第一个按钮的标签为 0,第二个按钮的标签为 1,依此类推。
在longPress
中获取用gesture.view
点击的按钮,获取标签。标签是您应该递增的数组的索引:
if let howMuch = textField.text {
self.totals[gesture.view!.tag] += Int(howMuch)! // Note: you should probably unwrap the Int? safely
self.viewWillAppear(true) // Note: you shouldn't really call this yourself
}