Changing the view's subclass


#1

Howdy y’all,

With some unwitting help from syndrome’s post in this same forum, I created a solution for Chapter 7’s Silver Challenge using a .xib file. However, I get a warning in my handler for the segmented-button tap action.

Here’s my handler method:

[code]- (IBAction)changeColor:(id)sender {
NSLog(@“Let’s change color! Here’s my view: %@”, [self view] );

// The following line generates a warning in Xcode
HypnosisView *view = [self view];

if ( [sender selectedSegmentIndex] == 0 ) {
    [view setCircleColor:[UIColor redColor]];
}
else if ( [sender selectedSegmentIndex] == 1 ) {
    [view setCircleColor:[UIColor greenColor]];
}
else {
    [view setCircleColor:[UIColor blueColor]];
}

}[/code]

The warning states “Incompatible pointer types initializing ‘HypnosisView *__strong’ with an expression of type ‘UIView *’”. This suggests to me that the controller doesn’t know that its ‘view’ property is an instance of class HypnosisView, not plain old UIView.

The program still runs, but I’d like it to run happier. Should I somehow tell the controller that its root view is a specific subclass, or should I leave that be and completely cover that root view with a new HypnosisView object? Or something else?


#2

The inherited view method’s return type is UIView, so anytime that method is called, the compiler believes it is going to be an instance of UIView and only UIView. The way around this compiler warning is to typecast the return value:

HypnosisView *v = (HypnosisView *)[self view];

Some classes, like UITableViewController, have a method that does this for you:

UITableView *tv = [self tableView];
UITableView *v = [self view];

In this code, v == tv, but assigning v generates a warning. The implementation of tableView looks like so:

- (UITableView *)tableView 
{
     return (UITableView *)[self view];
}