Here is my solution to the silver challenge.
The advantage of this solution compared to some other posted on this forum is that it follows the MVC pattern.
The objective was to not create the UISegmentedControl in the controller. When I tried to create it in the view, I put the creation code in drawRect: but each time the view is redrawn, another UISegmentedControl is created
I therefore found the following solution :
[ol][li]Create a xib file BNRHypnosisViewController.xib[/li]
[li]In interface builder, add a UIView[/li]
[li]In the inspector, change its class to BNRHypnosisView[/li]
[li]Change the File's Owner class to BNRHypnosisViewController[/li]
[li]Connect the view and the File's Owner (in both ways)[/li]
[li]Add a UISegmentedView with 3 segments named Red-Green-Blue, none of them selected[/li]
[li]Modify BNRHypnosisViewController to declare the outlet and action :
@interface BNRHypnosisViewController : UIViewController
@property (nonatomic, weak) IBOutlet BNRHypnosisView* hypnosisView;
- (IBAction)changeColor:(UISegmentedControl *)sender;
[li]Connect the action and the outlet in Interface Builder[/li]
[li]Modify BNRHypnosisView and put circleColor property in the header :
@interface BNRHypnosisView : UIView
@property (strong, nonatomic) UIColor *circleColor;
[li]Modify BNRHypnosisViewController implementation : remove loadView: and implement the action :
+ (NSArray *)colors
return @[[UIColor redColor], [UIColor greenColor], [UIColor blueColor]];
- (IBAction)changeColor:(UISegmentedControl *)sender
self.hypnosisView.circleColor = [BNRHypnosisViewController colors][sender.selectedSegmentIndex];
That's it !