Swift 4代表并传递文本字段数据

时间:2018-02-25 22:05:33

标签: swift delegates

我正在尝试使用GoogleBooks API制作应用,我可以使用标题或作者或两者来搜索图书。我目前正在处理委托部分,以便能够将搜索项传递给结果表视图。但是,我正在使用变量我得到的变量是常量,但我将它们声明为var,所以我不知道我在哪里弄乱。

这是视图的UIViewController代码,带有两个搜索框和按钮:

import UIKit

protocol ViewControllerDelegate: class {
    func searchInput(_ titleFromSearch: String?, authorFromSearch: String?)
}

class ViewController: UIViewController {
    @IBOutlet weak var titleFromSearch: UITextField!
    @IBOutlet weak var authorFromSearch: UITextField!

    weak var delegate: ViewControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        titleFromSearch.delegate = self
        authorFromSearch.delegate = self
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event:
        UIEvent?) {
        super.touchesEnded(touches, with: event)
        titleFromSearch.resignFirstResponder()
        authorFromSearch.resignFirstResponder()
    }
}

extension ViewController: UITextFieldDelegate {
    func fieldsDidEndEditing(_ titleEntered: UITextField, authorEntered:
        UITextField) {
        if let delegateController = delegate {
            delegateController.searchInput(titleFromSearch.text,
                                           authorFromSearch: authorFromSearch.text)
        }
    }
}

这是我为要显示的结果设置的TableViewController的代码。

import UIKit
import GoogleBooksApiClient

class SearchResultsTableViewController: UITableViewController {
    var titleFromSearch: String?
    var authorFromSearch: String?

    var data = [Volume]()

    override func viewDidLoad() {
        super.viewDidLoad()

        let session = URLSession.shared
        let client = GoogleBooksApiClient(session: session)

        let req = GoogleBooksApi.VolumeRequest.List(query: "Google")

        let task: URLSessionDataTask = client.invoke(
            req,
            onSuccess: { [weak self] volumes in
                self?.data = volumes.items
                self?.tableView.reloadData()
            },
            onError: { error in
                print("\(error)") }
        )
        task.resume()
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt
        indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",
                                                 for: indexPath)

        let item = data[indexPath.row]
        cell.textLabel?.text = item.volumeInfo.title
        cell.detailTextLabel?.text = item.volumeInfo.authors.first

        return cell
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        super.prepare(for: segue, sender: sender)
        if let destination = segue.destination as? ViewController {
            destination.delegate = self
        }
    }
}

这里是我得到let常量错误的地方是这两个赋值语句:

extension SearchResultsTableViewController: ViewControllerDelegate {
    func searchInput(_ titleFromSearch: String?, authorFromSearch: String?)
    {
        titleFromSearch = titleFromSearch
        authorFromSearch = authorFromSearch
    }
}

我添加了所有代码,因为正如我所说,我是iOS的新手,我不知道这个错误源于代码的位置。

1 个答案:

答案 0 :(得分:1)

导致错误的两行:

->

您正在尝试将参数分配给自己。您希望将参数值设置为您的同名属性。为此,请将titleFromSearch = titleFromSearch authorFromSearch = authorFromSearch 添加到属性引用:

self.