Cannot assign a value of type ViewController to a value of type UITextFieldDelegate?
Asked Answered
H

4

60

Here's the error when I wrote the line self.MessageTextField.delegate = self:

/ChatApp/ViewController.swift:27:42: Cannot assign a value of type 'ViewController' to a value of type 'UITextFieldDelegate?'

Here's my Swift code (ViewerController.swift):

//
//  ViewController.swift
//  ChatApp
//
//  Created by David Chen on 15/4/12.
//  Copyright (c) 2015年 cwsoft. All rights reserved.
//

import UIKit
import Parse

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var messagesArray:[String] = [String]()

    @IBOutlet weak var MessageTableView: UITableView!
    @IBOutlet weak var ButtonSend: UIButton!
    @IBOutlet weak var DockViewHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var MessageTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        //
        self.MessageTableView.delegate = self
        self.MessageTableView.dataSource = self
        //Set delegate
        self.MessageTextField.delegate = self

        self.messagesArray.append("Test 1")
        self.messagesArray.append("Test 2")
        self.messagesArray.append("Test 3")
    }

    @IBAction func ButtonSendPressed(sender: UIButton) {
        self.view.layoutIfNeeded()
        UIView.animateWithDuration(0.5, animations: {
            self.DockViewHeightConstraint.constant = 400
            self.view.layoutIfNeeded()
            }, completion: nil)
    }

    //MARK : TextField Delegage Methods


    //MARK : Table View Delegate Methods

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.MessageTableView.dequeueReusableCellWithIdentifier("MessageCell") as! UITableViewCell
        cell.textLabel?.text = self.messagesArray[indexPath.row]
        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messagesArray.count
    }
}
Helenhelena answered 13/4, 2015 at 12:35 Comment(2)
Sorry, my pc dead last night, I'll check it tonight. Now I gotta go to school. Thank you for your helpful answer @luk2302. And I'm learning swift by watching a tutorial on YouTube which in a channel called "Learn With Chris" :)Helenhelena
Xcode should detect this and popup a fix.Hip
T
101

The line self.MessageTextField.delegate = self causes the error since you try to assign self as the delegate of a UITextField.
But your ViewController is not a UITextFieldDelegate. To make your class this kind of delegte, you need to adopt the UITextFieldDelegate protocol. This can be achieved by adding it to the list of protocols and classes your class inherits from / conforms to. In your case that is done by changing the line

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

to

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate
Tapia answered 13/4, 2015 at 12:37 Comment(3)
i have the same error. Now added the UITextViewDelegate and the error does not go away... Anyone has a Solution?Cletuscleve
@Cletuscleve UITextViewDelegate is something else. The difference is Field vs. View!Tapia
@Tapia I read your comment above and it still didn't click what you meant. I finally figured it out. UITextViewDelegate vs UITextFieldDelegate. Not the same.Koblas
T
13

Declare that your class conforms to the UITextFieldDelegate protocol and implement any of those protocol methods that you need.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate { ... }
Teetotaler answered 13/4, 2015 at 12:37 Comment(0)
H
7

I had the same problem, but it was because of a different issue:

class CustomViewController: UIViewController {

    let customerCardTableView: UITableView = {
        let table = UITableView()
        table.translatesAutoresizingMaskIntoConstraints = false
        table.backgroundColor = UIColor(patternImage: UIImage(named: "background")!)
        table.delegate = self
        table.dataSource = self
        table.separatorStyle = .none
        table.separatorColor = UIColor(rgb:0xFFFFFF)
        table.separatorInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)

        return table
    }()

    ...other code 
}

and I did extend my CustomViewController to conform to the UITableViewDelegate, UITableViewDataSource protocols.

The reason was that self isn't accessible if you're using let. I had to lazy var customerCardTableView: UITableView i.e.:

lazy var customerCardTableView: UITableView = {
    let table = UITableView()
    table.translatesAutoresizingMaskIntoConstraints = false
    table.backgroundColor = UIColor(patternImage: UIImage(named: "background")!)
    table.delegate = self
    table.dataSource = self
    table.separatorStyle = .none
    table.separatorColor = UIColor(rgb:0xFFFFFF)
    table.separatorInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)

    return table
}()

lets are required to happen during instantiation, but lazy vars can be delayed to a time after instantiation, hence they can access self...

Howdy answered 6/12, 2017 at 17:43 Comment(0)
C
2

Embarrassing to own up to it, but worth a mention. Make sure that you're extending the correct view controller when declaring conformance. For example, don't do this by accident.

class ViewController: UIViewController {

}

extension SimilarlyNamedViewController: UITableViewDelegate {

}

It's an easy one to do if you use autocomplete and gets by you because, at a glance, it looks the same.

Coraciiform answered 10/5, 2019 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.