I am adapting a program from a book course that I have been following and have three classes: a view controller, a view and a singleton that houses a variable that is shared by the other two.
I am trying to write some code within the view controller that will subsequently trigger the view to send a [self setNeedsDisplay] message. However, when I set this up as an instance method, the compiler throws a 'no class method' exception. When I change the method to a class method, the 'self' property no longer works.
I'll be honest, I'm still getting my head around a lot of the distinctions of OOP in general, so I may not have a full grasp of the situation. But I was wondering, is it possible to send a message from one class to another that triggers the second class to initiate a 'self' action?
Here is the code in the view controller that would send the message (with my current coding attempt):
- (IBAction)indexChanged:(UISegmentedControl *)sender
{
switch (sender.selectedSegmentIndex)
{
case 0:
{
UIColor *selectedColor = [UIColor colorWithRed:1.0
green:0.0
blue:0.0
alpha:1.0];
NSLog(@"Red button pressed");
[BNRHypnosisView setCircleColor:selectedColor];
break;
}
...
Here is the code in the view class that would receive the message:
- (void)setCircleColor:(UIColor *)circleColor
{
[[CDFSingleton colorPicker] changeCircleColor:circleColor];
[self setNeedsDisplay];
}
Not sure how relevant it is, but here is my singleton class:
@implementation CDFSingleton
+ (CDFSingleton *) colorPicker
{
static CDFSingleton *colorPicker = nil;
if (!colorPicker) {
colorPicker = [[super allocWithZone:nil] init];
}
return colorPicker;
}
+ (id)allocWithZone:(struct _NSZone *)zone
{
return [self colorPicker];
}
- (id)init
{
self = [super init];
if (self) {
//In this case, set the default circleColor to light gray
_circleColor = [UIColor lightGrayColor];
NSLog(@"Initial colour set to light gray (%@)", _circleColor);
}
return self;
}
- (void)changeCircleColor:(UIColor *)color
{
_circleColor = color;
NSLog(@"Circle colour changed to %@", color);
NSLog(@"_circleColor currently set to %@", _circleColor);
}
@end
If anyone has any thoughts and/or opinions on this, I would love to hear them!