0

我有一组UIToolbar项目显示何时inputField成为第一响应者。

//... code
toolBar.items = [
            UIBarButtonItem(title: "Button 1", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(myFunction))]

myInputField.inputAccessoryView = toolBar

可以myFunction从上面的示例中使用参数调用吗?

就像是...

... #selector(myFunction(someParameter)))
4

1 回答 1

1

与 类似UIButton,可以通过 的tag属性传递参数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)")
}

在此示例中,点击按钮会打印按钮的标签:

Tapped: 1   // Tap button 1
Tapped: 2   // Tap button 2
于 2016-09-08T23:02:13.977 回答