Question

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!

Was it helpful?

Solution

OK, so, when you have two strings — say, @"kitten" and @"puppy" — if you wanted to get the length of a particular string, you would ask that string for its length. For example, you might write [@"kitten" length] and get the answer 6. You wouldn't write [NSString length], because you don't want the length of the NSString class — you want the length of that specific NSString that contains the word "kitten".

Your own classes work the same way. Just like you wouldn't talk to the NSString class when you want to know something about a particular NSString, you don't want to talk to the BNRHypnosisView class to affect the view being displayed in your app. Instead, you need to pass your view controller a reference to the specific BNRHypnosisView that is being displayed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top