我創建了類(customAccessaryView)並擴展了UIView
,我在UIToolbar
上顯示完成並取消按鈕,這是我從.xib
加載的。Swift 2.0:代理不工作
有關更多信息,請查找以下截圖。
在我customPickerView類我宣佈我的協議的方法和委託,我有綁定doneAction,並通過.xib
cancelAction。
類別:customAccessaryView
@objc protocol customAccessaryDelegate {
optional func clickONDone()
optional func clickONCancel()
}
class customAccessaryView : UIView {
var customView: UIView!
var delegate:customAccessaryDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
self.customView = NSBundle.mainBundle().loadNibNamed("CustomAccessary", owner: self, options: nil)[0] as? UIView
self.addSubview(self.customView)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBAction func doneAction(sender: AnyObject) {
if let delegate = self.delegate {
delegate.clickONDone?()
}
else {
print("Not Called")
}
}
@IBAction func cancelAction(sender: AnyObject) {
if let delegate = self.delegate {
delegate.clickONCancel?()
}
else {
print("Not Called")
}
}
}
當我將分配委託給另一個類(createEvent),則協議方法不調用。
類:createEvent
class createEvent : UIViewController, customAccessaryDelegate {
var txtFieldInstance: UITextField!
var customAV: customAccessaryView!
override func viewDidLoad() {
super.viewDidLoad()
self.customAV = customAccessaryView()
self.customAV?.delegate = self
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
self.txtFieldInstance = textField
textField.inputAccessoryView = self.customAV
return true
}
func clickONDone() {
self.txtFieldInstance.resignFirstResponder()
}
func clickONCancel() {
}
}
正如我所使用的customAccessaryView顯示的inputAccessoryView在鍵盤的處理完成,取消行動。
此外,我已經分配了委託並調用所需的方法,但它沒有調用它。
任何幫助,非常感謝。
更新
當我用力宣佈customAccessaryView
類的init
方法的委託,然後它工作。請看下面的代碼。
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.delegate = createEvent()
}
但我沒有強有力地提供它。
什麼是不正確的工作?在'doneAction()'中,打印出「Not Called」還是'doneAction()'甚至沒有調用? – Larme
我有更新了問題,實際上在createEvent類中調用了clickOne()方法,這個方法沒有調用哪個委託。 –