雨燕2.0的版本:
//Declared at top of view controller
var accessoryDoneButton: UIBarButtonItem!
let accessoryToolBar = UIToolbar(frame: CGRectMake(0,0,UIScreen.mainScreen().bounds.width, 44))
//Could also be an IBOutlet, I just happened to have it like this
let codeInput = UITextField()
//Configured in viewDidLoad()
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: #selector(self.donePressed(_:)))
self.accessoryToolBar.items = [self.accessoryDoneButton]
self.codeInput.inputAccessoryView = self.accessoryToolBar
斯威夫特4:
//Declared at top of view controller
var accessoryDoneButton: UIBarButtonItem!
let accessoryToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
//Could also be an IBOutlet, I just happened to have it like this
let codeInput = UITextField()
//Configured in viewDidLoad()
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.donePressed))
self.accessoryToolBar.items = [self.accessoryDoneButton]
self.codeInput.inputAccessoryView = self.accessoryToolBar
func donePressed() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
UIToolBar Documentation
'inputAccessoryView' documentation
感謝,但如果我想也從一個密鑰的方法調用? – Vins 2011-05-30 19:40:24
我沒有得到你。你想打甚麼? – 2011-05-30 19:49:44
我有一個將文本保存到數據庫的方法[self saveString]; – Vins 2011-05-30 19:51:47