如何在 swift 中为 xib 使用自定义委托?
How to use custom delegate for xib in swift?
我想使用 inputAccessoryView 在键盘上方添加自定义数字按钮。我已经添加了视图。我已将 class CustomKeyboard 分配给 xib 文件。在 CustomKeyboard class 文件中,我已经连接了按钮的 IBAction。我的 ViewController textField1 和 textFiled2 中有两个 textFields,并且我已将 inputAccessoryView 与 textField1 相关联。当我点击 textField1 时,我的自定义按钮会显示在键盘上方。我的目标是当我点击我的自定义按钮时,它应该在 textFild2 中输入值。但我真的不知道我将如何实现这一目标。任何人有一些想法请帮助我。
import Foundation
import UIKit
class CustomKeyboard:UIView {
@IBAction func rowButtonAction(_ sender: UIButton) {
print(sender.currentTitle)
}
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let view = UINib(nibName: "CustomKeyboard", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
textField1.inputAccessoryView = view//customView
textField1.autocorrectionType = .no
textField2.autocorrectionType = .no
}
}
您可以使用委托或闭包从 CustomKeyboard 获取回调到 ViewController。下面的示例使用委托方法,当用户点击视图时将调用函数 rowTapped。
import Foundation
import UIKit
protocol CustomKeyBoardProtocol:AnyObject{
fun rowTapped(title:String)
}
class CustomKeyboard:UIView {
weak var delegate:CustomKeyBoardProtocol?
@IBAction func rowButtonAction(_ sender: UIButton) {
print(sender.currentTitle)
delegate.rowTapped(title: sender.currentTitle)
}
}
import UIKit
class ViewController: UIViewController, CustomKeyBoardProtocol{
@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let customView = UINib(nibName: "CustomKeyboard", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomKeyboard
customView.delegate = self// he
textField1.inputAccessoryView = customView//customView
textField1.autocorrectionType = .no
textField2.autocorrectionType = .no
}
func rowTapped(title:String){//it will be called on tap
}
}
我想使用 inputAccessoryView 在键盘上方添加自定义数字按钮。我已经添加了视图。我已将 class CustomKeyboard 分配给 xib 文件。在 CustomKeyboard class 文件中,我已经连接了按钮的 IBAction。我的 ViewController textField1 和 textFiled2 中有两个 textFields,并且我已将 inputAccessoryView 与 textField1 相关联。当我点击 textField1 时,我的自定义按钮会显示在键盘上方。我的目标是当我点击我的自定义按钮时,它应该在 textFild2 中输入值。但我真的不知道我将如何实现这一目标。任何人有一些想法请帮助我。
import Foundation
import UIKit
class CustomKeyboard:UIView {
@IBAction func rowButtonAction(_ sender: UIButton) {
print(sender.currentTitle)
}
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let view = UINib(nibName: "CustomKeyboard", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
textField1.inputAccessoryView = view//customView
textField1.autocorrectionType = .no
textField2.autocorrectionType = .no
}
}
您可以使用委托或闭包从 CustomKeyboard 获取回调到 ViewController。下面的示例使用委托方法,当用户点击视图时将调用函数 rowTapped。
import Foundation
import UIKit
protocol CustomKeyBoardProtocol:AnyObject{
fun rowTapped(title:String)
}
class CustomKeyboard:UIView {
weak var delegate:CustomKeyBoardProtocol?
@IBAction func rowButtonAction(_ sender: UIButton) {
print(sender.currentTitle)
delegate.rowTapped(title: sender.currentTitle)
}
}
import UIKit
class ViewController: UIViewController, CustomKeyBoardProtocol{
@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let customView = UINib(nibName: "CustomKeyboard", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomKeyboard
customView.delegate = self// he
textField1.inputAccessoryView = customView//customView
textField1.autocorrectionType = .no
textField2.autocorrectionType = .no
}
func rowTapped(title:String){//it will be called on tap
}
}