Skip to content

Instantly share code, notes, and snippets.

@jamesaq12wsx
Created June 22, 2018 01:53
Show Gist options
  • Save jamesaq12wsx/d7465b8aadea1831b26d463f97e2aa3e to your computer and use it in GitHub Desktop.
Save jamesaq12wsx/d7465b8aadea1831b26d463f97e2aa3e to your computer and use it in GitHub Desktop.
[swift]建立App內部鍵盤
//1
protocol KeyboardDelegate: class {
func keyWasTapped(input: Int)
}
class MyKeyboard: UIView {
//2
@IBOutlet weak var Btn1: UIButton!
@IBOutlet weak var Btn2: UIButton!
@IBOutlet weak var Btn3: UIButton!
//3
// 這個Delegate 之後設定給 ViewController
// 鍵盤就可以給View Controller發訊息
weak var delegate: KeyboardDelegate?
// MARK:- 鍵盤初始化
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initializeSubviews()
}
override init(frame: CGRect) {
super.init(frame: frame)
initializeSubviews()
}
//4
func initializeSubviews() {
// 不需包括xib
let xibFileName = "MyKeyboard"
let view = Bundle.main.loadNibNamed(xibFileName, owner: self, options: nil)![0] as! UIView
self.addSubview(view)
view.frame = self.bounds
}
// MARK:- .xib 裏按鈕的Action
//5
@IBAction func keyTapped(sender: UIButton) {
// 按鈕按下後,發送訊息給
// delegate (也就是 view controller)
self.delegate?.keyWasTapped(input: sender.tag)
if sender.tag == 0 {
//6
Btn1.changeBtn(title: "AA", tag: 10)
Btn2.changeBtn(title: "AB", tag: 11)
Btn3.changeBtn(title: "AC", tag: 12)
}
else if sender.tag == 1{
Btn1.changeBtn(title: "BA", tag: 20)
Btn2.changeBtn(title: "BB", tag: 21)
Btn3.changeBtn(title: "BC", tag: 22)
}
else if sender.tag == 5 {
Btn1.changeBtn(title: "A", tag: 0)
Btn2.changeBtn(title: "B", tag: 1)
Btn3.changeBtn(title: "C", tag: 2)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment