         codeView.text = macros[selectedMacroIndex].script?.python
     }
 
+
+    override func viewWillDisappear(animated: Bool) {
+        super.viewWillDisappear(animated)
+
+        saveMacro(selectedMacroIndex)
+    }
+
     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }
 
 
+    /// A private method used to setup custom attributes for the codeview.
     private func setupCodeView() {
         self.codeView.backgroundColor = SyntaxHighlightThemes.Default.background
         self.codeView.textColor = SyntaxHighlightThemes.Default.plain


import UIKit

class EditMacroViewController: UIViewController {

    @IBOutlet weak var codeView: CodeView!
    @IBOutlet weak var macroSelector: UISegmentedControl!
    
    var macros: [PythonMacro] = []
    var selectedMacroIndex: Int = 0

    override func viewDidLoad() {


    
    @IBAction func macroSelectionChanged(sender: AnyObject) {
        saveMacro(macroSelector.selectedSegmentIndex)
        selectMacro(macroSelector.selectedSegmentIndex)
    }
    

    /// Method used to retieve the python script from the codeview.  Update
    /// the PythonMacro.  And then load the new Macro value.
    ///
    /// - parameter index: The index of the new PythonMacro to load into
    /// the CodeView
    func selectMacro(index: Int) {
        if index != selectedMacroIndex {
            codeView.text = macros[index].script?.python
            selectedMacroIndex = index
        }
    }


    /// Method used to save the python script in the CodeView back into
    /// its matching PythonMacro object.  The new script is then loaded back
    /// into the CPython runtime.
    ///
    /// - parameter index: The index of the PythonMacro to save.
    func saveMacro(index: Int) {
        macros[selectedMacroIndex].script?.python = codeView.text
        macros[selectedMacroIndex].registerMacro()
    }
}
