-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboard.swift
More file actions
55 lines (43 loc) · 1.71 KB
/
Copy pathKeyboard.swift
File metadata and controls
55 lines (43 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// Keyboard.swift
// KeyboardRiptutorial
//
// Created by Bill Morrison on 10/2/20.
//
// derived from https://stackoverflow.com/questions/33474771/a-swift-example-of-custom-views-for-data-input-custom-in-app-keyboard/33692231#33692231
// https://riptutorial.com/ios/example/16976/create-a-custom-in-app-keyboard
import UIKit
// The view controller will adopt this protocol (delegate)
// and thus must contain the keyWasTapped method
protocol KeyboardDelegate: class {
func keyWasTapped(character: String)
}
class Keyboard: UIView {
// This variable will be set as the view controller so that
// the keyboard can send messages to the view controller.
weak var delegate: KeyboardDelegate?
// MARK:- keyboard initialization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
/// After init'd ... initialize the subview, inputView, and delegate.
/// - Parameters:
/// - textField: textField description
/// - delegate: <#delegate description#>
func loadKeyboardNib(textField: UITextField, delegate: KeyboardDelegate) {
let xibFileName = "Keyboard" // xib extention not included
let view = Bundle.main.loadNibNamed(xibFileName, owner: self, options: nil)![0] as! Keyboard
self.addSubview(view)
view.frame = self.bounds
textField.inputView = self
self.delegate = delegate
}
// MARK:- Button actions from .xib file
@IBAction func buttonAction(_ sender: UIButton) {
guard let delegate = delegate else { return }
delegate.keyWasTapped(character: sender.titleLabel!.text!)
}
}