Search code examples
iosswifttableview

Cannot assign value of type 'ViewController' to type 'AddContactDelegate?'


import UIKit

struct Contact  {
    var fullname: String
    var contactNumber: String
}

class ViewController: UITableViewController {

    var contacts = [Contact]()
    @IBOutlet var tableview: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func handleAdd(_ sender: Any) {
        let controller = AddContacts()
        controller.delegate = self
        self.present(UINavigationController(rootViewController: controller), animated: true, completion: nil)
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = contacts[indexPath.row].fullname
        cell.detailTextLabel?.text = contacts[indexPath.row].contactNumber
        return cell

    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return contacts.count
    }
}


import UIKit

protocol AddContactDelegate {
    func addContact(contact: Contact)
}

class AddContacts: UIViewController {

    var delegate: AddContactDelegate?
    
    @IBOutlet weak var ContactTextField: UITextField!
    @IBOutlet weak var nameTextField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    @IBAction func save(_ sender: Any) {
        guard let fullname = nameTextField.text, nameTextField.hasText else {
            print("handle error here")
            return
        }
        
        guard let contactNumber = ContactTextField.text , ContactTextField.hasText else {
            print("enter contact error here")
            return
        }
        let contact  = Contact(fullname: fullname, contactNumber: contactNumber)
        print(contact.fullname)
        print(contact.contactNumber)
        delegate?.addContact(contact: contact)
    }
    
}

in viewController: UITableViewController file it shows error like Cannot assign value of type 'ViewController' to type 'AddContactDelegate?' what should do i do to solve these error


Solution

  • import UIKit
    
    struct Contact  {
        var fullname: String
        var contactNumber: String
    }
    
    class ViewController: UITableViewController {
    
        var contacts = [Contact]()
        @IBOutlet var tableview: UITableView!
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
        }
        
        @IBAction func handleAdd(_ sender: Any) {
            let controller = AddContacts()
            controller.delegate = self
            self.present(UINavigationController(rootViewController: controller), animated: true, completion: nil)
        }
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            cell.textLabel?.text = contacts[indexPath.row].fullname
            cell.detailTextLabel?.text = contacts[indexPath.row].contactNumber
            return cell
    
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return contacts.count
        }
    }
    

    // add this

    extension ViewController: AddContactDelegate {
          func addContact(contact: Contact) {
               contacts.append(contact)
               tableView.reloadData()
           }
    }
    

    // thats it

    import UIKit
    
    protocol AddContactDelegate:AnyObject {
        func addContact(contact: Contact)
    }
    
    class AddContacts: UIViewController {
    
        weak var delegate: AddContactDelegate?
        
        @IBOutlet weak var ContactTextField: UITextField!
        @IBOutlet weak var nameTextField: UITextField!
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
        
        @IBAction func save(_ sender: Any) {
            guard let fullname = nameTextField.text, nameTextField.hasText else {
                print("handle error here")
                return
            }
            
            guard let contactNumber = ContactTextField.text , ContactTextField.hasText else {
                print("enter contact error here")
                return
            }
            let contact  = Contact(fullname: fullname, contactNumber: contactNumber)
            print(contact.fullname)
            print(contact.contactNumber)
            delegate?.addContact(contact: contact)
        }
        
    }