2016-02-26 82 views
3

我創建了類(customAccessaryView)並擴展了UIView,我在UIToolbar上顯示完成並取消按鈕,這是我從.xib加載的。Swift 2.0:代理不工作

有關更多信息,請查找以下截圖。

enter image description here

在我customPickerView類我宣佈我的協議的方法和委託,我有綁定doneAction,並通過.xibcancelAction

類別: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() 
    } 

但我沒有強有力地提供它。

+0

什麼是不正確的工作?在'doneAction()'中,打印出「Not Called」還是'doneAction()'甚至沒有調用? – Larme

+0

我有更新了問題,實際上在createEvent類中調用了clickOne()方法,這個方法沒有調用哪個委託。 –

回答

1

編輯:這個答案是錯誤的:)我錯過了在textFieldShouldBeginEditing:行。


在你看來沒有加載你的線條

self.customAV = customAccessaryView() 
self.customAV?.delegate = self 

第一行不會做你期待什麼 - 我猜你想獲得customAccessoryView你從故事板創建設置它的代表?你實際上做的是創建一個新的CustomAccessoryView並設置它的委託。你永遠不會把它添加到你的視圖中,所以你委派的實例不是你在屏幕上可以看到的那個實例。

你需要讓你的財產的出口

@IBOutlet var customAV: customAccessaryView! 

而且在故事板全連接到你在那裏創建的視圖。然後刪除viewDidLoad中的第一行,以便始終使用故事板中的第一行。


NB這裏的一些(可能是不受歡迎)的建議是:在斯威夫特

類應以大寫字母即customAccessaryView確實應該CustomAccessaryView開始。協議也一樣。事物的實例以小寫字母開頭,因此當您閱讀代碼時,您可以輕鬆地將它們分開。

您已將委託方法設爲可選,但在調用它時使用!。如果你打算這麼做,爲什麼要在協議中使它成爲可選項呢? (或者如果你希望它是可選的,請使用delegate.clickONDone?()來代替

+0

感謝您的答案和有關創建類的指南。正如您剛纔提到的「您永遠不會將它添加到您的視圖中」一樣,在「inputAccessoryView」中顯示,所以它可以考慮與否,請糾正我,如果我錯了 –

+0

嗯,這意味着我的答案是錯誤的:)接下來的事情我會看看你的委託是否仍然正確設置CustomAccessaryView - 當你點擊按鈕時,你的控制檯中是否出現「Not Called」? – deanWombourne

+0

我沒有說你的答案是錯的,但它對我不起作用。是的,我在控制檯中看到「Not Called」。請如果你有時間,請看看它,因爲我在最近3到4天掙扎着,並不知道它爲什麼不起作用。 –