Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Connecting the Controller with the Model

Create an instance of the converter class inside the ConverterController class in Xcode.

  1. In the Classes folder in the Groups and Files sidebar, double-click ConverterController.h to open it in an editor window.

  2. Declare a pointer to a converter object by adding the following line to your code right after the outlets are declared, before the ending bracket:

    Converter *converter;

    When clicked, the Convert button sends the convert: message to the ConverterController object. Complete the definition of the convert: method in the ConverterController so that it sends the convertCurrency message to the Converter instance to execute the conversion:

  3. Import Converter.h so ConverterController can instantiate a Converter object. Add the following line under the first import statement in ConverterController.h.

    #import "Converter.h"
  4. In the Classes group, double-click ConverterController.m to open this file in an editor window.

  5. Insert the highlighted lines in Listing 5-1 into ConverterController.m.



    Listing 5-1  Definition of the convert: method in ConverterController.m

    #import "ConverterController.h"
    @implementation ConverterController
    - (IBAction)convert:(id)sender {
        float amount;
        converter = [[Converter alloc]init]; // 1
        [converter setSourceCurrencyAmount:[dollarField floatValue]]; // 2
        [converter setRate:[rateField floatValue]]; // 2
        amount =   [converter convertCurrency]; // 3
     
        [amountField setFloatValue:amount]; // 4
        [rateField selectText:self]; // 5
    }
    @end

The convert: method does the following:

  1. Initializes a Converter object.

  2. Sets the member variables of the Converter class to the values in the rateField and dollarField text fields.

  3. Sends the convertCurrency message to the object pointed to by the converter pointer and gets the returned value.

  4. Uses setFloatValue: to write the returned value to the Amount in Other Currency text field (amountField).

  5. Sends the selectText: message to the rate field. As a result, any text in the field is selected; if there is no text, the insertion point is placed in the text field so the user can begin another calculation.

Each code line in the convert: method, excluding the declaration of floating-point variables, is a message. The “word” on the left side of a message expression identifies the object receiving the message (called the receiver). These objects are identified by the outlets you defined and connected. After the receiver comes the name of the method that the sending object (called the sender) wants the receiver to execute. Messages often result in values being returned; in the above example, the local variable amount holds a returned value.



< Previous PageNext Page > Hide TOC


Last updated: 2007-10-31




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice