O'Reilly logo

Learning Cocoa by Apple Computer Inc

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implement the Classes of Travel Advisor

Now that the user interface for Travel Advisor has been created and the connections between objects specified in Interface Builder, you can proceed with the implementation of the application’s classes.

Reuse Currency Converter

Using Interface Builder, you have already set up an action so that clicking the Convert button will invoke TAController’s convertCurrency: method. Now all you have to do to is write the code to get numeric values from the user, invoke the Converter object using those values, and send the result back to the user interface for display:

  1. Open TAController.m by clicking it in Project Builder’s Groups & Files view.

  2. At the top of the file, add the following line. This gives TAController access to the Converter class’s methods:

    #import "Converter.h"
  3. Now modify the empty declaration of the convertCurrency: method, as shown. This is not the most compact form you can use to express a solution in Objective-C, but it clearly delineates the steps involved in solving this problem. First, the values that the user entered are retrieved from the user interface. Next, the converter object’s convertAmount:atRate: method is invoked to perform the conversion operation. Finally, the value of currencyLocalField is set to reflect the result returned by the Converter object:

    - (IBAction)convertCurrency:(id)sender { float rate, dollars, result; dollars = [currencyDollarsField floatValue]; rate = [currencyRateField floatValue]; result = [converter ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required