Joe Joe - 4 months ago 39
iOS Question

Swift Error : UITableViewController has no member "function name"

I been stuck with error , i'm trying to open date picker through textfield , showing an error that UItableViewController has no member "function name"

import Foundation
import UIKit
class tableview : UITableViewController {
@IBOutlet weak var dateTextField: UITextField!
@IBAction func textFieldEditing(sender: UITextField) {

let datePickerView:UIDatePicker = UIDatePicker()

datePickerView.datePickerMode = UIDatePickerMode.Date

sender.inputView = datePickerView

datePickerView.addTarget(self, action: #selector(UITableViewController.datePickerValueChanged), forControlEvents: UIControlEvents.ValueChanged)
}
func datePickerValueChanged(sender:UIDatePicker) {

let dateFormatter = NSDateFormatter()

dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle

dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle

dateTextField.text = dateFormatter.stringFromDate(sender.date)
**strong text**
}


enter image description here

Answer

Make proper name to class tableview.(tableview -> TableViewController).

For add target you have to write class nmae.so here is class name (tableview -> TableViewController)

If you are using IBOutlet for textfield then put inputView code in viewDidLoad.

Try below code.

    import Foundation
    import UIKit
    class TableViewController : UITableViewController {

       @IBOutlet weak var dateTextField: UITextField!

       func viewDidLoad(){

           super.viewDidLoad()
           let datePickerView:UIDatePicker = UIDatePicker()

           datePickerView.datePickerMode = UIDatePickerMode.Date

           dateTextField.inputView = datePickerView

           datePickerView.addTarget(self, action: #selector(TableViewController.datePickerValueChanged), forControlEvents: UIControlEvents.ValueChanged)
}

    @IBAction func textFieldEditing(sender: UITextField) {


    }
    func datePickerValueChanged(sender:UIDatePicker) {

        let dateFormatter = NSDateFormatter()

        dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle

        dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle

        dateTextField.text = dateFormatter.stringFromDate(sender.date)
        **strong text**
    }
Comments