0

I have a table view. In that I am creating the UITextField dynamically. But I am not able to fire the events in it.

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)
}

Please suggest how to assign the delegate to the UITextField in this scenario so that I can fire an event to show a view to pick some values from it.

The error is:

"Cannot assign the value of ViewName(classname) to type UITextFieldDelegate"

on the line:

dynamicTxtField1.delegate = self
rmaddy
  • 314,917
  • 42
  • 532
  • 579
DEV
  • 949
  • 1
  • 9
  • 29
  • 2
    What is the error you are getting? Does your `self` conform to `UITextFieldDelegate`? – Grimxn Sep 13 '16 at 17:48
  • When posting a question about an error, you need to include the complete and exact error message in your question. – rmaddy Sep 13 '16 at 17:55
  • Error is "Cannot assign the value of ViewName(classname) to type UITextFieldDelegate". Self is not referring to UITextFieldDelegate that is what the problem is. What should i do now? – DEV Sep 13 '16 at 18:17
  • I made a mistake of adding UiTextViewDelegate instead of UiTextFieldDelegate. But i have corrected it . Still the events are not fired. dynamicTxtField1.addTarget(self, action: "ButtonPressed", for: UIControlEvents.touchDown) Will this work for a UITextField????? – DEV Sep 13 '16 at 18:45

1 Answers1

0

enter image description hereYou should create your textField in your cell's subclass then modify its attributes in your cellForRowAtIndexPath. Does your class implement UITextFieldDelegate?

EDIT: try this

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

and here is the method

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

}

Mat Grlt
  • 131
  • 8
  • I made a mistake of adding UiTextViewDelegate instead of UiTextFieldDelegate. But i have corrected it . Still the events are not fired. dynamicTxtField1.addTarget(self, action: "ButtonPressed", for: UIControlEvents.touchDown) Will this work for a UITextField????? – DEV Sep 13 '16 at 18:44
  • Of you want to call a method each time you write a letter you need your own method and use .ValueChanged. UITextFieldDelegate does not handle it. – Mat Grlt Sep 13 '16 at 18:53
  • Can you please give me an example how to write it? I want the method on Tap . Because i want to go to another view to show a table view as soon as user taps on the textField – DEV Sep 13 '16 at 18:55
  • Hi Mat, I tried the way you mentioned. Here is my code. dynamicTxtField1.addTarget(self, action: Selector(DynamicSuperView.textFieldChanged(_:)), for: UIControlEvents.editingChanged) func textFieldChanged(textField: UITextField) { print("TextField Changed!!!!!!!!!!!!") } It throws an error saying textFieldChanged is not a member of this class – DEV Sep 13 '16 at 19:10
  • It works? Is DynamicSuperView a UIViewController? – Mat Grlt Sep 13 '16 at 19:13
  • Its not working. Yes it is a UIViewController. If you can see the intellisense will strikeout the method we have created. I have edited the post with link to the error message. I don't think you will be able to see the strike off image is it is pending peer review. – DEV Sep 13 '16 at 19:16
  • http://i.stack.imgur.com/oz6CO.png – DEV Sep 13 '16 at 19:23
  • Check again my edit and try this code ty – Mat Grlt Sep 13 '16 at 19:40
  • Still no luck. It still says textFieldChanged is not a member – DEV Sep 13 '16 at 20:27
  • I changed my code to dynamicTxtField1.addTarget(self, action: #selector(DynamicSuperView.textFieldChanged(textField:)), for: UIControlEvents.valueChanged) Now its not giving the compilation error but it is not executing the textFiledChanged function – DEV Sep 13 '16 at 20:43
  • textField.addTarget(self, action: #selector(onTextChange), forControlEvents: UIControlEvents.EditingChanged) This solved the problem!!!!! – DEV Sep 17 '16 at 19:32