ios - swift 3 从父 ViewController 调用函数

标签 ios class swift3 protocols uicontainerview

我有一个 ViewController,这个 View 容器有一个创建 2 个容器 View 的类,并向第一个容器添加一个表,为第二个容器添加一个 HashtagPicker。

hashTagPicker 有一个函数,只要所选的 hashTag 发生更改,就会调用该函数。

问题 :每当标签更改时,我想调用更新表函数。如何从包含容器的类中定义的 hashtagclass 调用函数?

最佳答案

我个人喜欢委托(delegate)方法而不是通知——后一种解决方案几乎总是会导致架构困惑。可悲的是,委托(delegate)方法的示例(也是公认的答案)更糟糕 - 它基本上为内存泄漏提供了机会。我会解释的。在接受的解决方案中,ParentView强烈推荐 HashtagPicker反过来,HastagPicker强烈推荐 ParentView ,这会创建一个保留周期,这意味着两个 Controller 都不会被 ARC 拾取并被取消初始化。因此,例如,如果您正在展示 ParentView从其他角度来看,您继续前往ParentView再回来,您将继续产生 ParentView 的新实例(和 HashtagPicker )旧的仍然占用内存。

现在,这应该怎么做。我将使用与接受的答案完全相同的名称。

协议(protocol)应该这样定义:

// note the ": class" part
protocol HashTagPickerDelegate: class {
  func picked(hashtag: String)
}

如果我们指定 class ,这意味着该协议(protocol)只能在类上使用。这将允许使用创建弱引用,否则这是不可能的。
class HashtagPicker: UIViewController {
  // if HashTagPickerDelegate wouldn't be limited to class, 
  // we couldn't have made a weak reference here!
  weak var delegate: HashTagPickerDelegate?

  // at some point, you call the delegate, it can be anywhere, this is just an example
  @IBAction func tappedHashtag(_ sender: Any) {
    delegate?.picked(hashtag: "bla")
  }
}

现在我们对委托(delegate)有一个弱引用,所以没有保留循环,ARC可以很好地清理一切!

我将输入其余代码以作为完整答案:
class ParentView: UIViewController {
  func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // we are presenting the nested controller
    if segue.identifier == "SegueHastagPickerContainer",
       let destinationController = segue.destination as? HashtagPicker {
      destinationController.delegate = self
    } 
  }
}

extension ParentView: HashTagPickerDelegate {
  func picked(hashtag: String) {
    // we just got info from the child controller, do something with it!
  }
}

关于ios - swift 3 从父 ViewController 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40715210/

相关文章:

ios - 第二次单击 UITableView 时应用程序崩溃

iOS 如何从解析 Swift 4 中获取值 "Boolean"

ios - 如何根据平移手势速度将角脉冲应用于 Sprite Kit 节点

java - 通过类名动态实例化 Fragment

c++ - 作为类成员的函数指针

arrays - fatal error : Index out of Range Swift 3. 1

swift - 展开通用类型

ios - 如何在 ios 中使用 Mediaplayer 框架播放视频?

ruby - 我怎样才能 "replace"通过 ruby​​ include 函数包含的模块

Swift,自动调整自定义表格 View 单元格的大小