简体   繁体   English

Swift委托函数未从协议中调用

[英]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. 我正在使用一种协议来调用Books类列表中的委托函数,但是并未调用此函数。 The function not being called is newBook. 未调用的函数是newBook。 My code seems fine to me, yet the newBook delegate function is not being called. 我的代码对我来说似乎很好,但是没有调用newBook委托函数。 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. 由于某些原因未调用newBook。

You need to set the addBookDelegate for AddBookViewController as CurrentBooksTableViewController 您需要将AddBookViewController的addBookDelegate设置为CurrentBooksTableViewController

Inside CurrentBooksTableViewController before presenting AddBookViewController you need to set addBookDelegate to self. 在出现AddBookViewController之前,先在CurrentBooksTableViewController内部,您需要将addBookDelegate设置为self。

INSTANCE_OF_ AddBookViewController.addBookDelegate = self

Then only you can access the addBookDelegate functions from other side. 然后,只有您可以从另一端访问addBookDelegate函数。 otherwise if you check addBookDelegate will be = nil 否则,如果您检查addBookDelegate将为= nil

Have you set the value of addBookDelegate ? 您是否设置了addBookDelegate的值? You can do this in the prepare for segue function in your table view controller. 您可以在表视图控制器中的“准备segue”功能中执行此操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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