Thiết lập một tập tin nib với cách bố trí inputView thích hợp và các mặt hàng. Trong trường hợp của tôi, tôi đặt từng nút thành một tác vụ trên Chủ sở hữu tệp là inputViewButtonPressed
.
Thiết lập bảng phân cảnh (hoặc nib nếu bạn thích) cho bộ điều khiển chế độ xem.
Sau đó sử dụng đoạn mã sau, bạn sẽ nhận được những gì bạn đang tìm kiếm:
class ViewController: UIViewController, UITextFieldDelegate {
var myInputView : UIView!
var activeTextField : UITextField?
override func viewDidLoad() {
super.viewDidLoad()
// load input view from nib
if let objects = NSBundle.mainBundle().loadNibNamed("InputView", owner: self, options: nil) {
myInputView = objects[0] as UIView
}
// Set up all the text fields with us as the delegate and
// using our input view
for view in self.view.subviews {
if let textField = view as? UITextField {
textField.inputView = myInputView
textField.delegate = self
}
}
}
func textFieldDidBeginEditing(textField: UITextField) {
activeTextField = textField
}
func textFieldDidEndEditing(textField: UITextField) {
activeTextField = nil
}
@IBAction func inputViewButtonPressed(button:UIButton) {
// Update the text field with the text from the button pressed
activeTextField?.text = button.titleLabel?.text
// Close the text field
activeTextField?.resignFirstResponder()
}
}
Ngoài ra, nếu bạn đang muốn có nhiều bàn phím giống như, bạn có thể sử dụng chức năng này như hành động của bạn (nó sử dụng các cú pháp let mới từ Swift 1.2), phá vỡ nó lên nếu bạn cần 1.1 tương thích:
@IBAction func insertButtonText(button:UIButton) {
if let textField = activeTextField, title = button.titleLabel?.text, range = textField.selectedTextRange {
// Update the text field with the text from the button pressed
textField.replaceRange(range, withText: title)
}
}
này sử dụng giao thức UITextInput
để cập nhật các trường văn bản cho phù hợp.Xử lý xóa phức tạp hơn một chút, nhưng vẫn không quá tệ:
@IBAction func deleteText(button:UIButton) {
if let textField = activeTextField, range = textField.selectedTextRange {
if range.empty {
// If the selection is empty, delete the character behind the cursor
let start = textField.positionFromPosition(range.start, inDirection: .Left, offset: 1)
let deleteRange = textField.textRangeFromPosition(start, toPosition: range.end)
textField.replaceRange(deleteRange, withText: "")
}
else {
// If the selection isn't empty, delete the chars in the selection
textField.replaceRange(range, withText: "")
}
}
}
Tạo tệp lớp tùy chỉnh và đặt làm lớp cho xib của bạn trong InterfaceBuilder. Sau đó, bạn có thể kéo ctrl từ các phần tử giao diện người dùng vào tệp lớp của bạn (trình soạn thảo trợ lý) để tạo IBOutlets. Sử dụng các cửa hàng này trong mã của bạn để cập nhật các phần tử giao diện người dùng. – zisoft
Tôi đã có thể tạo IBOutlets/IBActions cho các nút trong customView. Điều này là cần thiết nhưng không đủ để nhận các nút trong customView để giao tiếp textInput/keyboardFunctionality với khung nhìn của TextInputs. – codycoats
quan điểm của bạn có phải triển khai/tuân thủ đúng giao thức không? (UITextInput?). bạn cũng có thể phải nói với họ để trở thành người trả lời đầu tiên khi họ được khai thác. Tôi muốn thử subclassing UIControl vs UIView – nielsbot