Debian Codegraph @

iOS/Csound iOS Swift Examples/Csound iOS SwiftExamples/BaseCsoundViewController.swift

raw
1 /*
2
3 BaseCsoundViewController.swift
4
5 Nikhil Singh, Dr. Richard Boulanger
6 Adapted from the Csound iOS Examples by Steven Yi and Victor Lazzarini
7
8 This file is part of Csound iOS SwiftExamples.
9
10 The Csound for iOS Library is free software; you can redistribute it
11 and/or modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
14
15 Csound is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public
21 License along with Csound; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 02111-1307 USA
24
25 */
26
27 import UIKit
28
29 // Define base class for all examples
30 class BaseCsoundViewController: UIViewController {
31
32 var infoText = ""
33 var infoVC = UIViewController()
34 var csound = CsoundObj() // CsoundObj() property inherited by all examples
35
36 var detailItem: AnyObject? {
37 didSet(newDetailItem) {
38 configureView()
39
40 if masterPopoverController != nil {
41 masterPopoverController?.dismiss(animated: true, completion: nil)
42 }
43 }
44 }
45 var detailDescriptionLabel: UILabel!
46 var masterPopoverController: UIViewController?
47
48 override func viewDidLoad() {
49 if UIDevice.current.userInterfaceIdiom == .phone {
50 self.navigationItem.leftBarButtonItem = splitViewController?.navigationItem.leftBarButtonItem
51 }
52 super.viewDidLoad()
53 }
54
55 override func viewDidDisappear(_ animated: Bool) {
56 super.viewDidDisappear(animated)
57 csound.stop()
58 }
59
60 override func didReceiveMemoryWarning() {
61 super.didReceiveMemoryWarning()
62 // Dispose of any resources that can be recreated.
63 }
64
65 func configureView() {
66 if detailItem != nil {
67 detailDescriptionLabel.text = detailItem?.description
68 }
69 }
70
71 // Method to display information in a popover, called from each inheriting class
72 func displayInfo(_ sender: UIButton) {
73 infoVC.modalPresentationStyle = .popover
74
75 let infoTextView = UITextView(frame: CGRect(x: 0, y: 0, width: infoVC.preferredContentSize.width, height: infoVC.preferredContentSize.height))
76 infoTextView.isEditable = false
77 infoTextView.isSelectable = false
78
79 infoTextView.attributedText = NSAttributedString(string: infoText)
80 infoTextView.font = UIFont(name: "Menlo", size: 16)
81
82 let popover = infoVC.popoverPresentationController
83 infoVC.view.addSubview(infoTextView)
84 popover?.delegate = self
85 popover?.permittedArrowDirections = .up
86 popover?.sourceView = sender
87 popover?.sourceRect = sender.bounds
88 present(infoVC, animated: true, completion: nil)
89 }
90
91 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
92 segue.destination.modalPresentationStyle = .popover
93 }
94
95 // MARK: Init
96 required override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
97 super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
98 }
99
100 required init?(coder aDecoder: NSCoder) {
101 super.init(coder: aDecoder)
102 }
103 }
104
105 // MARK: UISplitViewControllerDelegate
106 extension BaseCsoundViewController: UISplitViewControllerDelegate {
107 func splitViewController(_ svc: UISplitViewController, willChangeTo displayMode: UISplitViewControllerDisplayMode) {
108 navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem;
109 navigationItem.leftBarButtonItem?.title = "Csound for iOS"
110 masterPopoverController = svc
111 }
112
113 func splitViewController(_ svc: UISplitViewController, willShow aViewController: UIViewController, invalidating barButtonItem: UIBarButtonItem) {
114 navigationItem.leftBarButtonItem = nil
115 masterPopoverController = nil
116 }
117 }
118
119 // MARK: UIPopoverPresentationControllerDelegate
120 extension BaseCsoundViewController: UIPopoverPresentationControllerDelegate {
121 func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
122 return .none // Ensure infoVC presents as popover on iPhone
123 }
124
125 func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
126 return .none
127 }
128 }