将完成处理程序添加到presentViewControllerAsSheet(NSViewController)?

2023-12-01

我试图呈现一个工作表配置视图(AddSoundEffect)对于我的主窗口/视图控制器(我正在使用故事板),当配置视图控制器被关闭时,采用在AddSoundEffect查看并将其传递回主视图。我当前在主视图控制器中的代码:

presentViewControllerAsSheet(self.storyboard!.instantiateControllerWithIdentifier("AddSoundEffect") as! AddSoundViewController

并且在AddSoundViewController.swift文件,关闭它的代码是:

self.dismissViewController(self)

为了传递数据,我有一个与类无关的元组,用于保存数据。如何添加完成处理程序presentViewControllerAsSheet,并且(可选)是否有更好的方法在视图控制器之间传递数据?

设置:Xcode 版本 6.4,OS X 10.10.4


代表团模式对您来说是最简单的方法。

// Replace this with your tuple or whatever data represents your sound effect
struct SoundEffect {}

protocol AddSoundViewControllerDelegate: class {
  func soundViewController(controller: AddSoundViewController, didAddSoundEffect: SoundEffect)
}

//
// Let's say this controller is a modal view controller for adding new sound effects
//
class AddSoundViewController: UIViewController {
  weak var delegate: AddSoundViewControllerDelegate?

  func done(sender: AnyObject) {
    // Dummy sound effect info, replace it with your own data
    let soundEffect = SoundEffect()

    //
    // Call it whenever you would like to inform presenting view controller
    // about added sound effect (in case of Done, Add, ... button tapped, do not call it
    // when user taps on Cancel to just dismiss AddSoundViewController)
    //
    self.delegate?.soundViewController(self, didAddSoundEffect: soundEffect)

    // Dismiss self
    self.dismissViewControllerAnimated(true, completion: {})
  }
}

//
// Let's say this controller is main view controller, which contains list of all sound effects,
// with button to add new sound effect via AddSoundViewController
//
class SoundEffectsViewController: UIViewController, AddSoundViewControllerDelegate {
  func presentAddSoundEffectController(sender: AnyObject) {
    if let addSoundController = self.storyboard?.instantiateViewControllerWithIdentifier("AddSoundEffect") as? AddSoundViewController {
      addSoundController.delegate = self
      self.presentViewController(addSoundController, animated: true, completion: {})
    }
  }

  func soundViewController(controller: AddSoundViewController, didAddSoundEffect: SoundEffect) {
    // This method is called only when new sound effect is added
  }
}

另一种方法是使用闭包:

// Replace this with your tuple or whatever data represents your sound effect
struct SoundEffect {}

//
// Let's say this controller is a modal view controller for adding new sound effects
//
class AddSoundViewController: UIViewController {
  var completionHandler: ((SoundEffect) -> ())?

  func done(sender: AnyObject) {
    // Dummy sound effect info, replace it with your own data
    let soundEffect = SoundEffect()

    //
    // Call it whenever you would like to inform presenting view controller
    // about added sound effect (in case of Done, Add, ... button tapped, do not call it
    // when user taps on Cancel to just dismiss AddSoundViewController)
    //
    self.completionHandler?(soundEffect)

    // Dismiss self
    self.dismissViewControllerAnimated(true, completion: {})
  }
}

//
// Let's say this controller is main view controller, which contains list of all sound effects,
// with button to add new sound effect via AddSoundViewController
//
class SoundEffectsViewController: UIViewController {
  func presentAddSoundEffectController(sender: AnyObject) {
    if let addSoundController = self.storyboard?.instantiateViewControllerWithIdentifier("AddSoundEffect") as? AddSoundViewController {
      addSoundController.completionHandler = { [weak self] (soundEffect) -> () in
        // Called when new sound effect is added
      }
      self.presentViewController(addSoundController, animated: true, completion: {})
    }
  }
}

或者许多其他方式,例如发送通知,...无论您需要什么。但在这种特定情况下,委托模式或闭包是最好的方法。


我错过了你的问题是关于NSViewController。此示例适用于 iOS,但相同的模式可以在 OS X 上使用,没有任何问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将完成处理程序添加到presentViewControllerAsSheet(NSViewController)? 的相关文章

随机推荐

  • Powershell 来分割大型 csv。需要很长时间。希望通过代码审查来缩短流程

    csv 大约是一个演出 有超过 100 万行 我正在尝试将其分成 5 个 前 4 个大约需要 18 小时 第 5 个永远不会发生 或者我没有足够的耐心 我的电脑将处于睡眠模式 因为它不会解锁 function Split Csv Cmdle
  • 如果我使用 Javascript 和 CSS 动态获取数据库的文本,如何将文本划分为列?

    我认为一个脚本使得每个容器中一定数量的字符都可以工作 你知道这样的脚本吗 这是一种响应式设计 我可以使用多个 css 文件 但我只需要一个 html 文件 问题出在我将动态获取的文本中 所以我不知道我会得到多少个字符 在桌面大小中 段落排列
  • fgets() 没有像我期望的那样工作

    谁能告诉我为什么这段代码不起作用 当我运行时 它只是打印出 输入有关线索 1 的信息 并且没有任何输入 跳到另一个步骤 include
  • Windows 物理驱动器访问 fopen 和 fseek

    我目前正在尝试以 C 语言中的二进制数据流的形式访问物理硬盘 我已经安装了一个映像 img 并且可以从操作系统 Win 7 中读取它 我的 C 程序只是尝试以只读二进制模式打开物理驱动器 然后从驱动器读取一些数据 但是 如果我只是从流中读取
  • 将计算列添加到 pandas 中的数据框

    我有一个 OHLC 价格数据集 我已将其从 CSV 解析为 Pandas 数据框 并重新采样为 15 分钟柱
  • 验证 Thymeleaf 中的输入

    我有这个输入 Masa
  • Javascript多维数组返回错误的维度[重复]

    这个问题在这里已经有答案了 我这样初始化一个数组 imgArray imgArray 0 0 image1 imgArray 1 0 image2 imgArray 0 1 image3 imgArray 1 1 image4 imgArr
  • Java 程序的程序计数器寄存器值

    是否可以获取正在运行的 Java 应用程序 尤其是在 Android 模拟器中运行的应用程序 的程序计数器 PC 寄存器值 例如 gprof 会随机采样 PC 寄存器值 以便找出哪个函数正在消耗程序的运行时间 这样的概念对 JVM 有意义吗
  • 优化零重力二维空间中粒子的引力计算

    我用 python 创建了一个小的粒子可视化 我正在计算零重力二维空间中粒子的运动 每个粒子都会根据粒子质量和距离吸引所有其他粒子 我在 pygame 中做了一个可视化 一切都按计划进行 通过计算 但是我需要极大地优化计算 如今 该系统可以
  • Python 多处理和 wxPython 协同工作

    我有以下问题 我编写了一个脚本 该脚本最多同时运行四个进程 通过命令行使用它时 它就像一个魅力 然后我决定使用 wxPython 编写一个 GUI 并且我很快发现 GUI 和脚本需要在不同的进程中运行 以便在另一个执行某些操作时两者都保持可
  • C# WCF:在提供对服务的访问的共享库中拥有单个 app.config

    我目前有一个包含几个项目的解决方案 其中之一是 WCF 服务 我创建了另一个带有静态类的项目 该静态类基本上提供了通往 WCF 客户端实例的网关 如下所示 public static class WSGateway public stati
  • PostgreSQL - 加入 string_agg

    我有三张桌子 Students student id name 1 Rhon Subjects subject id subject name student id 1 Physics 1 2 Math 1 Grades grade id
  • 如何从Python中的字典中提取所有值?

    我有一本字典d 1 0 3246 2 0 9185 3 3985 如何提取所有值d到一个列表中l 如果您只需要字典键1 2 and 3 use your dict keys 如果您只需要字典值 0 3246 0 9185 and 3985
  • 将玩家移动到精确的触摸/鼠标点击位置

    在我的 2D unity 游戏中 我试图将精灵移动到触摸 光标的位置 现在是单击鼠标 我的精灵位于 173 48 52 1 位置 然而 当我单击一个可能距离几个像素的位置时 我的位置会更改为 399 129 0 并且我的精灵显然被扔进了广阔
  • 获取元素的 XPath 列表[重复]

    这个问题在这里已经有答案了 我有具有特定名称的元素的 NodeList 并且我希望拥有所有这些节点的 XPath 我找不到如何做到这一点的方法 NodeList nList doc getElementsByTagName definiti
  • Laravel5 - 非静态方法不应静态调用

    我不知道这个错误是什么 请有人给我一些解释 on my UserController php class UserController extends Controller public function viewCard card id
  • 使用 PHP 合并图像时的图像透明度和 Alpha

    所以我在 PHP Doc 上找到了一些代码 并对其进行了稍微编辑以合并我拥有的两个图像 然后图像将保存在服务器上的文件夹中 但是有一个小问题 我无法弄清楚为什么会发生这种情况 首先我的代码 glassurl GET GlassImg fra
  • 有没有办法只执行文档测试,忽略打印函数调用?

    假设来说 我的功能返回一个值 and 有很多打印语句 也许 100 个或更多 有没有办法跑doctest这样所有其他打印工作都可以被忽略 跳过 我熟悉 SKIP指令 用于跳过doctest示例 即当我执行我的函数 或将我的模块作为脚本运行
  • 使用本地设置进行 django 测试

    Python 2 7 姜戈1 2 当我测试 Django 应用程序时 我遇到了奇怪的 local settings 行为 我有我的
  • 将完成处理程序添加到presentViewControllerAsSheet(NSViewController)?

    我试图呈现一个工作表配置视图 AddSoundEffect 对于我的主窗口 视图控制器 我正在使用故事板 当配置视图控制器被关闭时 采用在AddSoundEffect查看并将其传递回主视图 我当前在主视图控制器中的代码 presentVie