我有一组UIToolbar
个项目,显示inputField
何时成为第一响应者。
//... code
toolBar.items = [
UIBarButtonItem(title: "Button 1", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(myFunction))]
myInputField.inputAccessoryView = toolBar
是否可以使用参数?
从上面的示例中调用myFunction
像...一样的东西。
... #selector(myFunction(someParameter)))
答案 0 :(得分:1)
与UIButton
类似,参数可以通过tag
的{{1}}属性传递。
UIBarButtonItem
点击按钮可打印此示例中的按钮标记:
override func viewDidLoad() {
super.viewDidLoad()
let toolBar = UIToolbar()
let firstButton = UIBarButtonItem(title: "Button 1", style: UIBarButtonItemStyle.Done, target: self, action: #selector(myFunction))
let secondButton = UIBarButtonItem(title: "Button 2", style: UIBarButtonItemStyle.Done, target: self, action: #selector(myFunction))
firstButton.tag = 1
secondButton.tag = 2
toolBar.items = [ firstButton, secondButton ]
toolBar.sizeToFit()
textField.inputAccessoryView = toolBar
}
func myFunction(sender: UIBarButtonItem) {
print("Tapped: \(sender.tag)")
}