PaperController Class Modifications

Now it’s time to finish the PaperController class. To do this, we need to make the following changes to PaperController:

  • Add a method that waits for the user to hit the Return key, gets the new formula that the user has typed, and then sends it to the appropriate Evaluator process.

  • Add a method that gets invoked when there is data from the Evaluator process ready to be displayed in the NSTextView.

  • Terminate the Evaluator process when a MathPaper window is closed.

Creating the NSTextView Delegate

There are many ways to find out when the user hits the Return key. One way is to subclass the NSTextView class and examine each event; another is to set up a delegate object that will be alerted each time the text inside the NSTextView changes. If the change results from a carriage return, our delegate can then grab the last line of the NSTextView object and send that line to Evaluator.

An NSTextView delegate can receive all sorts of special messages when things happen to the NSText object. The one that we care about here is the textDidChange: message, which is sent to the delegate object whenever the text changes. To find out if the text changed because the user hit the Return key, our delegate method asks the window for the current event; it then asks the current event for its characters and checks whether the resulting string is equal to a carriage return.

After the MathPaper user hits the Return key, our delegate method asks the NSTextView object for ...

Get Building Cocoa Applications: A Step by Step Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.