Silver Challenge - please review



This is my first post to the bulletin board - so if I mess something up procedural-wise, please let me know.

Anyway, I'd like to share with the group my solution to chapter 7's Silver challenge.

Here are the changes I made ...

1) In HypnosisViewController.h - I added the following method declaration ...


2) In HypnosisViewController.m ...

I added the code indicated below to the 'loadView' method ...

	- (void) loadView
	    // create a view
	    CGRect frame = [[UIScreen mainScreen] bounds];
	    HypnosisView *v = [[HypnosisView alloc] initWithFrame:frame];

	    // begin Silver Challenge code
	    NSArray * itemsArray = [NSArray arrayWithObjects:@"Red",@"Green",@"Blue",nil];
	    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems: itemsArray ] ;
	    segmentedControl.frame = CGRectMake(35, 20, 250, 50);
	    segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
	    segmentedControl.selectedSegmentIndex = 0;
	    segmentedControl.tag = 10;
	    [segmentedControl addTarget:self
	    [v addSubview:segmentedControl];
	    [v setCircleColor:[UIColor redColor]];                

	    // end Silver Challenge code

	    // set 'v' as *the* view of this view controller
	    [self setView:v];

and I added the 'changeFilter:sender' method ...

	    HypnosisView * view = (HypnosisView *) [self view];
	    UISegmentedControl *segmentedControl = (UISegmentedControl *)[view viewWithTag:10];
	    NSInteger index = [segmentedControl selectedSegmentIndex];
	    UIColor *color = [UIColor redColor];
	    if (index == 1) {
	        color = [UIColor greenColor];
	    else if (index == 2) {
	        color = [UIColor blueColor];
	    [view setCircleColor:color];                

That's it.  

Its been a couple of days since I worked on the challenge - so I may have missed something, but I think that's it.  

My thinking, with respect to memory / memory leaks is that since the 'segmentedControl' is a 'child' view of the HypnosisView instance ('v' in code) - before 'v' is deallocated by the iOS, any of its children (of which 'segmentedControl' is certainly one) will also be deallocated.

Please let me know if I missed anything.

For what its worth - I ran 'Analyze' on the project and no warnings were issued.  Not sure if that's proof that there are no leaks or not ???



Thanks for this, I found this very helpful. I like the way you only explained the additions you made. I am new to this after having worked through an objective-c book so I am still struggling with some of the commands. I am unfamiliar with the following syntax:

HypnosisView * view = (HypnosisView *) [self view];
UISegmentedControl *segmentedControl = (UISegmentedControl *)[view viewWithTag:10];

Is this shorthand for what would be a number of separate commands?