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

Mat*_*att 2 macos event-handling nsviewcontroller swift

我正在尝试AddSoundEffect为我的主窗口/视图控制器(我正在使用故事板)提供工作表配置视图(),并且当配置视图控制器被解除时,获取在AddSoundEffect视图中输入的值并将其传递回主视图.我在主视图控制器中的当前代码:

presentViewControllerAsSheet(self.storyboard!.instantiateControllerWithIdentifier("AddSoundEffect") as! AddSoundViewController
Run Code Online (Sandbox Code Playgroud)

AddSoundViewController.swift文件中,解雇它的代码是:

self.dismissViewController(self)
Run Code Online (Sandbox Code Playgroud)

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

设置:Xcode版本6.4,OS X 10.10.4

zrz*_*zka 5

委派模式是最简单的方法.

// 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
  }
}
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用闭包:

// 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: {})
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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


我错过了你的问题NSViewController.此示例适用于iOS,但可以在OS X上使用相同的模式而不会出现任何问题.