简体   繁体   中英

Swift delegate function not being called from protocol

I am making a library of sorts. One screen, a table view controller, contains a list of books and the other screen, a view controller, allows you to add books.

I'm using the Add Book class to add books to the list of books screen. The add books screen contains text inputs to allow user to enter the book's name, author, publisher etc. When a user clicks a button after entering those fields, it creates a book object containing those attributes, and the user is directed back to the home screen showing the new book added to the list of books. The list of books is a table of rows. Each row contains one book respectively.

I'm using a protocol to call the delegate function in the list of books class, however it's not calling this function. The function not being called is newBook. My code seems fine to me, yet the newBook delegate function is not being called. Please help!. I've added relevant code below:

Add Book class:

import UIKit
protocol AddBookProtocol {
    func newBook(book: Book)
}
class AddBookViewController: UIViewController {
    var addBookDelegate: AddBookProtocol?

    @IBOutlet weak var authorField: UITextField!
    @IBOutlet weak var genreField: UITextField!
    @IBOutlet weak var editionField: UITextField!
    @IBOutlet weak var isbnField: UITextField!
    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var dateField: UITextField!
    @IBOutlet weak var publisherField: UITextField!
    @IBOutlet weak var descriptionField: UITextField!

    @IBAction func saveButton() {
        print("HAHHHAHAHAH")
        let name = nameField.text
        let isbn = isbnField.text
        let author = authorField.text
        let publisher = publisherField.text
        let publishDate = dateField.text
        let genre = genreField.text
        let edition = editionField.text
        let desc = descriptionField.text
        let book = Book(title: name!, isbn: isbn!, author: author!, publishDate: publishDate!, genre: genre!, publisher: publisher!, edition: edition!, desc: desc!)
        addBookDelegate?.newBook(book: book)
        navigationController?.popViewController(animated: true)

    }
 //rest of the code

Class displaying list of current books:

class CurrentBooksTableViewController: UITableViewController, AddBookProtocol {
     var BooksList: [Book] = []
 func newBook(book: Book) {
        BooksList.append(book)
        tableView.beginUpdates()
        tableView.insertRows(at: [IndexPath(row: BooksList.count - 1, section: 0)], with: .automatic)
        tableView.endUpdates()

        tableView.reloadSections([SECTION_COUNT], with: .automatic)
    }
 //rest of the code
}

newBook is not being called for some reason.

You need to set the addBookDelegate for AddBookViewController as CurrentBooksTableViewController

Inside CurrentBooksTableViewController before presenting AddBookViewController you need to set addBookDelegate to self.

INSTANCE_OF_ AddBookViewController.addBookDelegate = self

Then only you can access the addBookDelegate functions from other side. otherwise if you check addBookDelegate will be = nil

Have you set the value of addBookDelegate ? You can do this in the prepare for segue function in your table view controller.

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let addBookVC = segue.destination as? AddBookViewController {
        addBookVC.addBookDelegate = self
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM