2016-09-13 61 views
0

我有一個表視圖。我正在動態創建UITextField。 但我無法解決其中的事件。無法委託分配到的UITextField,因爲它是在泰伯維

func tableView(_ tableView: UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell { 
     let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! DynamicTableCell 

     var dynamicTxtField1: UITextField = UITextField() 
     dynamicTxtField1.backgroundColor = UIColor.white 
     dynamicTxtField1.rightViewMode = .always 
     dynamicTxtField1.layer.cornerRadius = 5; 
     dynamicTxtField1.clipsToBounds = true 
     dynamicTxtField1.borderStyle = .roundedRect 
     // Here i am not able to assign the 
     //Error in this line // dynamicTxtField1.delegate = self 

     // Here object "self" is referring to the view. Because of this i am not able to fire any events of the UITextField 
     cell.addSubview(dynamicTxtField1) 
} 

請建議如何委託到的UITextField分配在這種情況下,這樣我可以觸發事件,以顯示視圖從中挑選一些價值。

的錯誤是:

「無法分配VIEWNAME(類名)的值鍵入UITextFieldDelegate」 就行了

dynamicTxtField1.delegate = self 
+2

什麼是錯誤您收到?你的'self'符合'UITextFieldDelegate'嗎? – Grimxn

+0

發佈有關錯誤的問題時,需要在問題中包含完整的確切錯誤消息。 – rmaddy

+0

錯誤是「無法將ViewName(classname)的值分配爲鍵入UITextFieldDelegate」。 自我並不是指UITextFieldDelegate就是問題所在。我現在應該怎麼做? – Dev

回答

0

enter image description here您應該創建textField在你的單元格的子類中,然後在你的cellForRowAtIndexPath中修改它的屬性。你的班級是否實施UITextFieldDelegate

編輯:試試這個

 textField.addTarget(self, action: #selector(ViewController.textfieldDidchange(_:)), forControlEvents: .ValueChanged) 
     cell.contentView.addSubview(textField) 

這裏是方法

func textfieldDidchange(textField: UITextField) { 
//your code 

}

+0

我犯了一個錯誤,添加UiTextViewDelegate而不是UiTextFieldDelegate。但我已糾正它。事件仍然未被解僱。 dynamicTxtField1.addTarget(個體經營,行動: 「ButtonPressed」,爲:UIControlEvents.touchDown) 將這項工作對UITextField ????? – Dev

+0

你想在每次寫信時調用方法,你需要自己的方法並使用.ValueChanged。 UITextFieldDelegate不處理它。 –

+0

你能給我舉個例子怎麼寫嗎? 我想要Tap上的方法。因爲我想轉到另一個視圖來顯示錶格視圖,只要用戶點擊textField – Dev