Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [42]

By Root 911 0
must be called setFido:. Note that this is more than a just a convention; if you give your accessors nonstandard names, they will not get called by the key-value coding methods. Add fido and setFido: to KvcFunApp-Delegate.m:

- (int)fido

{

NSLog(@"-fido is returning %d", fido);

return fido;

}

- (void)setFido:(int)x

{

NSLog(@"-setFido: is called with %d", x);

fido = x;

}

Declare these methods in KvcFunAppDelegate.h:

- (int)fido;

- (void)setFido:(int)x;

Build and run the application. Note that your accessor methods are being called.

Bindings


Many graphical objects in Cocoa have bindings. When you bind a key, such as fido, to an attribute of a graphical object, such as its value or its font color, the view will automatically keep those in sync. You are going to add a slider, bind its value to fido, and see how it uses key-value coding to keep them in sync.

Open MainMenu.xib. Drop a slider on the window. In the Attributes Inspector, make the slider Continuous (Figure 7.1).

Figure 7.1. Make Slider Continuous

In the Bindings Inspector, bind the value of the slider to the fido key of the instance of KvcFunAppDelegate (Figure 7.2).

Figure 7.2. Bind Value of Slider to fido

Build and run the application. Note that the slider uses valueForKey: to get its initial value (which triggers your fido method). As you move the slider, it calls setValue:forKey: to update the value of fido (which triggers your setFido: method).

Key-Value Observing


What happens if fido is changed by something other than the slider? How would the slider know that it has a new value?

When the slider is created, it tells the KvcFunAppDelegate that it is observing its fido key. Whenever the value of fido is changed by the accessor methods or by key-value coding, the KvcFunAppDelegate sends a message to the slider notifying it that fido has changed.

In MainMenu.xib again, add a Label text field to the window, and bind its value to KvcFunAppDelegate’s fido key (Figure 7.3).

Figure 7.3. Bind Value of Label to fido

Build and run the app. Note that when you move the slider, setFido: is called. This notifies the text field that fido has changed. The text field uses valueForKey: to get the new value of fido. Thus, you see the fido method getting called.

Making Keys Observable


The previous section mentioned that when you use accessors or key-value coding to change the value for a key, the observers are automatically notified of the change. What happens if you change the variable directly?

Open KvcFunAppDelegate.h and declare a new action method:

- (IBAction)incrementFido:(id)sender;

In KvcFunAppDelegate.m, implement the method:

- (IBAction)incrementFido:(id)sender

{

fido++;

NSLog(@"fido is now %d", fido);

}

Open MainMenu.xib. Add a button to the window, label it Increment Fido, and Control-drag from the button to the instance KvcFunAppDelegate. The button should trigger the incrementFido: action (Figure 7.4).

Figure 7.4. Set Target and Action of Button

You would hope that when the button is clicked, the slider would move and the text field would update itself. Sadly, neither happens. Try building and running the application.

If you are going to change the variable directly, you will need to explicitly trigger the notification of the observers. Change the incrementFido: method:

- (IBAction)incrementFido:(id)sender

{

[self willChangeValueForKey:@"fido"];

fido++;

NSLog(@"fido is now %d", fido);

[self didChangeValueForKey:@"fido"];

}

Build and run the application now; the Increment Fido button should work correctly.

There are two other solutions that would work. First, you could use key-value coding:

- (IBAction)incrementFido:(id)sender

{

NSNumber *n = [self valueForKey:@"fido"];

NSNumber *npp = [NSNumber numberWithInt:[n intValue] + 1];

[self setValue:npp forKey:@"fido"];

}

Or you could use the accessor method to change fido:

- (IBAction)incrementFido:(id)sender

{

[self setFido:[self fido] + 1];

}

Type this version in.

Return Main Page Previous Page Next Page

®Online Book Reader