PlatformViewController.swift 1.63 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import Cocoa

7 8 9 10 11 12 13 14 15 16 17
/**
`ViewControllers` in the xib can inherit from this class to communicate with
the flutter view for this application. ViewControllers that inherit from this
class should be displayed as a popover or modal, with a button that binds to
the IBAction `pop()`.

To get the value of the popover during close, pass a callback function as
the `dispose` parameter. The callback passed will have access to the
`PlatformViewController` and all of it's properties at close so that the `count`
can be passed back through the message channel if needed.
*/
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
class PlatformViewController: NSViewController {
    var count: Int = 0

    var dispose: ((PlatformViewController)->())?

    @IBOutlet weak var label: NSTextField!

    var labelText: String {
      get {
        return "Button tapped \(self.count) time\(self.count != 1 ? "s" : "")."
      }
    }

    override func viewDidLoad() {
      super.viewDidLoad()
      self.label.stringValue = labelText
    }

    public required init?(coder aDecoder: NSCoder) {
      self.count = 0
      self.dispose = nil
      super.init(coder: aDecoder)
    }

    init(withCount count: Int, onClose dispose: ((PlatformViewController)->())?) {
      self.count = count
      self.dispose = dispose
      super.init(nibName: nil, bundle: nil)
    }

    @IBAction func pop(_ sender: Any) {
      self.dispose?(self)
      dismiss(self)
    }

    @IBAction func increment(_ sender: Any) {
      self.count += 1
      self.label.stringValue = labelText
    }
}