Silver Challenge Solution and a few questions


Here is my Silver Challenge Solution:


- (void)loadView
    // Create a view
    CGRect frame = [UIScreen mainScreen].bounds;
    // Create a YANHypnosisView
    YANHypnosisView *hypnosisView = [[YANHypnosisView alloc] initWithFrame:frame];
    // Create UISegmentedControl
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Red", @"Green", @"Blue"]];
    [segmentedControl addTarget:hypnosisView action:@selector(segmentedControlOption:) forControlEvents:UIControlEventValueChanged];
    segmentedControl.frame = CGRectMake((frame.size.width / 2.0 - segmentedControl.frame.size.width / 2), 35,
                                        segmentedControl.frame.size.width, segmentedControl.frame.size.height);
    segmentedControl.backgroundColor = [UIColor whiteColor];
    // Add subviews
    self.view = hypnosisView;
    [self.view addSubview:segmentedControl];


- (void)segmentedControlOption:(UISegmentedControl *)segment
    switch (segment.selectedSegmentIndex) {
        case 0:
            self.circleColor = [UIColor redColor];
        case 1:
            self.circleColor = [UIColor greenColor];
        case 2:
            self.circleColor = [UIColor blueColor];

The size of the control frame hasn’t the best implementation but it did the work. and I used as the target of the UIsegmentedControl my YANHypnosisView instance because circleColor is a Class extension property, I didn’t want to change this property to be in the header file but I don’t know what is the better solution. The other question is about the backgroundColor, I used whiteColor but the control looks awful because the white color is out of the round-shape corners, any good solution to have a white color for the segments but not in the round corners?

My last question is about this part of my code:

self.view = hypnosisView; [self.view addSubview:segmentedControl];

Is this ok? I don’t know if I mess the UI hierarchy here.

For YANHypnosisView.m I created a method to use in the [UIsegmentedControl addTarget:action:@selector()forControlEvents:] and then I used a switch for all the segments indexes.


I’m not the expert (actually I kind of took your solutions as hint for mine), still… don’t you think that moving the control logic into the View would be like taking some attribution from the Controller? (actually looks like the View is targeting for itself)

I move the circleColor property from private to public at the View and used the Controller as a target (that is the reason I moved the circleColor property to public.

Just my opinion, I’m not the guru :slight_smile: