title: Swift - How to use a closure to fire a function in a view model?
body: <p>I am watching the video series </p>
  
  <p>Swift Talk #5
  Connecting View Controllers
  url: <a href="https://talk.objc.io/episodes/S01E05-connecting-view-controllers" rel="nofollow noreferrer">https://talk.objc.io/episodes/S01E05-connecting-view-controllers</a></p>
  
  <p>In this video series they remove all the <code>prepareForSegue</code> and use an <code>App</code> class to handle the connection between different view controllers.</p>
  
  <p>I want to replicate this, but specifically only in my current view model; but what I don't get is how to connect view controllers through a view model (or even if you're meant to)</p>
  
  <p>In their code, at github: <a href="https://github.com/objcio/S01E05-connecting-view-controllers/blob/master/Example/AppDelegate.swift" rel="nofollow noreferrer">https://github.com/objcio/S01E05-connecting-view-controllers/blob/master/Example/AppDelegate.swift</a></p>
  
  <p>They use do this within their view controller</p>
  
  <p><code>var didSelect: (Episode) -&gt; () = { _ in }</code></p>
  
  <p>This runs;</p>
  
  <pre><code>func showEpisode(episode: Episode) {
          let detailVC = storyboard.instantiateViewControllerWithIdentifier("Detail") as! DetailViewController
          detailVC.episode = episode
          navigationController.pushViewController(detailVC, animated: true)
      }
  </code></pre>
  
  <p>In the same way, I want to use my ViewController to use my ViewModel for a menu button press (relying on tag).</p>
  
  <p>My code follows;</p>
  
  <pre><code>struct MainMenuViewModel {
      enum MainMenuTag: Int {
          case newGameTag = 0
      }
  
      func menuButtonPressed(tag: Int) {
          guard let tagSelected = MainMenuTag.init(rawValue: tag) else {
              return
          }
          switch tagSelected {
          case .newGameTag:
              print ("Pressed new game btn")
              break
          }
      }
  
      func menuBtnDidPress(tag: Int) {
          print ("You pressed: \(tag)")
          // Do a switch here 
          // Go to the next view controller?  Should the view model even know about navigation controllers, pushing, etc?
      }
  }
  
  class MainMenuViewController: UIViewController {
  
      @IBOutlet var mainMenuBtnOutletCollection: [UIButton]!
  
      var didSelect: (Int) -&gt; () = { _ in }
  
      override func viewDidLoad() {
          super.viewDidLoad()
      }
  
      @IBAction func mainMenuBtnPressed(_ sender: UIButton) {
          let tag = (sender).tag
          self.didSelect(tag)
  
      }
  }
  </code></pre>
  
  <p>What I don't understand is how do I connect the command</p>
  
  <p><code>self.didSelect(tag)</code> </p>
  
  <p>to the function</p>
  
  <p><code>func menuButtonPressed(tag: Int)</code> </p>
  
  <p>within my ViewModel</p>
  
  <p>As I understand it, according to the swift talk video is that the idea is that the view controller are "plain" and that the view model handles all the major stuff, like menu button presses and then moving to different view controllers as necessary.  </p>
  
  <p>How do I connect the didSelect item to my viewModel function?</p>
  
  <p>Thank you.</p>