This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ViewController 繼承 KeyboardDelegate | |
ViewController: UIViewController, KeyboardDelegate { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// 初始化鍵盤 | |
let keyboardView = MyKeyboard(frame: CGRect(x: 0, y: 0, width: 0, height: 300)) | |
// 當鍵盤有按鍵按下後,會通知ViewController | |
keyboardView.delegate = self | |
// 設定input為自定義鍵盤 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol SwitchKeyboardDelegate: class { | |
func switchKeyboard() | |
} | |
class CustomAccessoryView: UIView { | |
weak var delegate: SwitchKeyboardDelegate! | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func switchKeyboard() { | |
print("switch keyboard") | |
textField.endEditing(true); | |
if textField.inputView == nil { | |
textField.inputView = myKeyBoard | |
}else{ | |
textField.inputView = nil | |
} | |
textField.becomeFirstResponder() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1 | |
protocol KeyboardDelegate: class { | |
func keyWasTapped(input: Int) | |
} | |
class MyKeyboard: UIView { | |
//2 | |
@IBOutlet weak var Btn1: UIButton! | |
@IBOutlet weak var Btn2: UIButton! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
//... | |
let keyboardAccessoryView = CustomAccessoryView(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) | |
keyboardAccessoryView.delegate = self | |
textField.inputAccessoryView = keyboardAccessoryView | |
} |