ios - 更改 navigationItem.titleView 的委托(delegate)方法不起作用

标签 ios swift uinavigationcontroller delegates

我正在尝试根据 CollectionViewController 中选择的行更改 ListViewControllernavigationItem.titleView。我正在尝试使用委托(delegate)方法来执行此操作,但它不起作用。 didSelectRowAt 正在被调用,但是 func changeTitleView 永远不会被调用。我不确定出了什么问题。当我在 CollectionViewController 中使用 func changeTitleView 并在它自己的类中使用它时,它起作用了,所以我知道该函数正在起作用。我只是不完全确定如何将从 CollectionVC 中选择的 String 数据传递到 ListVC。我对委托(delegate)方法非常陌生。

全局变量:

var selectedCollection: CollectionItem?

Collection View Controller :

protocol HandleTitleView {
  func doesThisWork(answer: String)
}

class CollectionViewController: UITableViewController {

  var handleTitleViewDelegate: HandleTitleView? = nil

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let selectedItem = collections[indexPath.row]
    selectedCollection = selectedItem
    let title = selectedCollection?.collectionTitle

    handleTitleViewDelegate?.doesThisWork(answer: title!)
  }
}

ListView Controller :

class ListViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // set up the collectionViewDelegate
    let collectionViewController = storyboard?.instantiateViewController(withIdentifier: "CollectionViewController") as! CollectionViewController
    collectionViewController.titleView = titleLabel
    collectionViewController.handleTitleViewDelegate = self
}

extension ListViewController: HandleTitleView {

  func doesThisWork(answer: String) {
    print(answer)

  }

}

感谢您为我提供的任何帮助。提前致谢。

更新

我通过名为 MenuTableViewController 的侧边菜单实例化 CollectionViewController。我现在意识到我需要链接委托(delegate)方法,但是,CollectionViewController 嵌入在导航栏中,因此当我实例化它时,我必须实例化 CollectionViewControllerNav 并且无法设置MenuTableViewController 作为 CollectionVC 委托(delegate):

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    switch indexPath.row {

    case 0:

            let collectionViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CollectionViewController") as! CollectionViewController
            collectionViewController.handleTitleViewDelegate = self

            let collectionViewControllerNav = storyboard?.instantiateViewController(withIdentifier: "CollectionViewControllerNav")
            self.present(collectionViewControllerNav!, animated: true, completion: nil)

在这种情况下我不知道如何设置委托(delegate)方法

更新

当我呈现 CollectionViewController 而不是其对应的 CollectionViewControllerNav 时,委托(delegate)方法会起作用

最佳答案

扩展不能/不应该覆盖。

如 Apple 的 Swift 指南中所述,无法覆盖扩展中的功能(如属性或方法)。

Extensions can add new functionality to a type, but they cannot override existing functionality.
Apple Developer Guide

编译器允许您在扩展中重写以与 Objective-C 兼容。但这实际上违反了语言指令。

而不是为 didSelectrow 扩展 CollectionViewcontroller 。你应该实现 didSelectRow 方法是 CollectionViewController

关于ios - 更改 navigationItem.titleView 的委托(delegate)方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46629365/

相关文章:

ios - 如何更改 "back"栏按钮项的标题?

ios - 为什么带有惰性变量注册单元类的 UICollectionView 会抛出错误?

ios - 在 UITableView 中使用动画在顶部插入行

ios - 在 ParallaxBlur Controller 中刷新 TableView

ios - 一个横向 Controller

ios - 在 Objective-C 中定义变量、setter 和 getter

iOS 重定向到设置主页

ios - Q re : cleaning up an an array without moving indexes

ios - 图像在约束条件下未按预期工作

iphone - 如何使用uinavigationcontroller