1

I created two classes 1UIViewControllerClass and its 1UIViewClass (Which is the View of the ViewController). On my UIViewClass I have two methods, one of them is touchesBegan, which is getting the location of the touched point. The second Methode, which only runs on the View, takes the location information and gets the color at this location in view. The second Methode returns with an UIColor.

After these steps the UIColor should be send to the UIViewController. I tried to get the UIColor Variable to the ViewController (Delegate and Instanzing), but nothing worked.

The code is below.

Update: Tried one answer but it did not work. Updated this code.

Here is FarbView.h:

#import <UIKit/UIKit.h>

@protocol FarbDelegate <NSObject>
@required
- (void)receiveNewColor:(UIColor*)color;
@end


@interface FarbView :UIView {
    __weak id <FarbDelegate> delegate;
}
@property (nonatomic, weak) id <FarbDelegate> delegate;

@property (strong,nonatomic) UIColor *pickedColor;

- (UIColor *) colorOfPoint:(CGPoint)point;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

@end

Here is FarbView.m:

#import "FarbView.h"
#import <QuartzCore/QuartzCore.h>

@implementation FarbView
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



//Get Location of touched Point
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    self.pickedColor = [[UIColor alloc]init];
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint loc = [touch locationInView:self];
    NSLog(@"%@", NSStringFromCGPoint(loc));

    self.pickedColor = [self colorOfPoint:loc];

    //if you will describe receiveNewColor method on your view controller class we send new color message.
    if([delegate respondsToSelector:@selector(receiveNewColor:)]){
        [delegate receiveNewColor:self.pickedColor];
    }
}



//Getting Color at Location
- (UIColor *) colorOfPoint:(CGPoint)point
{
    unsigned char pixel[4] = {0};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

    [self.layer renderInContext:context];

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]);

    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];

    return color;
}

Next is FarbViewController.h

#import <UIKit/UIKit.h>
#import "FarbView.h"

@interface FarbViewController:UIViewController <FarbDelegate>

@property (strong, nonatomic) IBOutlet UILabel *currentColor;
@property (strong, nonatomic) FarbView *farbview;

-(void)receiveNewColor:(UIColor *)color;
@end

And FarbViewController.m

#import "FarbViewController.h"

@interface FarbViewController ()

@end

@implementation FarbViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"Richtige Page 1");

    self.farbview =[[FarbView alloc]init];
    self.farbview.delegate = self;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)receiveNewColor:(UIColor *)color{
    NSLog(@"New color selected %@", color);
    //your code here
}
@end
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102

1 Answers1

3

I don't recommend to use NSNotificationCenter here.

The best way to receive callback from child is Delegate pattern.

FarbView.h file:

@protocol FarbDelegate <NSObject>
@required
- (void)receiveNewColor:(UIColor*)color;
@end


@interface FarbView :UIView{
    __weak id <FarbDelegate> delegate;
    //...
}
@property (nonatomic, weak) id <FarbDelegate> delegate;

FarbView.m touch began handler:

//Get Location of touched Point
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    self.pickedColor = [[UIColor alloc]init];
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint loc = [touch locationInView:self];
    NSLog(@"%@", NSStringFromCGPoint(loc));

    self.pickedColor = [self colorOfPoint:loc];

    //if you will describe receiveNewColor method on your view controller class we send new color message.
    if([delegate respondsToSelector:@selector(receiveNewColor:)]){
        [delegate receiveNewColor:self.pickedColor];
    }

NSLog(@"Color: %@",self.pickedColor);
}

In ViewController class add declaration of method receiveNewColor:

-(void)receiveNewColor:(UIColor)color{
    NSLog(@"New color selected %@", color);
    //your code here
}

And don't forget add in viewDidLoad method next line of code:

//self.farbView - its your object of FarbView class    
self.farbView.delegate = self;

Here you will have warning. Just add "FarbDelegate" into @interface line :

@interface FarbViewController:UIViewController<FarbDelegate>
Vadim A.
  • 46
  • 2
  • Okay i tried it but it doesn't work either. Send the Code as an answer: – user2738907 Sep 02 '13 at 09:55
  • Ok. May be touchesBegan method doesn't work? It could happened if your view interaction is off. Check in interface builder for flag "user interaction enabled". It should be on. Also If you add farbView through Interface Builder, check do you select Custom class for your farbView (its on "identity inspector" on right part of Interface Builder). – Vadim A. Sep 02 '13 at 11:52
  • I checked it, "user interaction enabled" is on and Custom class "FarbView" is selected. I also tried to use it on Xcode 4, did not work too. So I thought maybe it is an project issue, in this case I opened a new Project, didn't work. Any other ideas what I could do? – user2738907 Sep 02 '13 at 13:05
  • Did you used debug? Does program enter to touchesBegan method? Also Be sure that "user interaction enabled" is "on" for all parent views for your FarbView – Vadim A. Sep 02 '13 at 13:58
  • when you make `self.farbView.delegate = self` is `self.farbView` not **NULL**? And did `[delegate respondsToSelector:@selector(receiveNewColor:)]` return **YES**? On touchesBegan delegate should not be NULL also. – Vadim A. Sep 03 '13 at 07:17
  • You have been right. I tried to log out the Method, but the Delegate didnt response. So the problem must be that `[delegate respondsToSelector:@selector(receiveNewColor:)]` didn't return **YES**. So I commend out the if statement and tried to only use `[delegate receiveNewColor:self.pickedColor];` but it didn't worked. How to solve this issue? And how can i test if self.farbview isn't **NULL**? (I'm not a Pro in coding with delegates) – user2738907 Sep 03 '13 at 11:41
  • Do you have in Interface builder view for farbview? If yes, remove `self.farbview =[[FarbView alloc]init];` line of code from viewDidLoad method on FarbViewController.m. Than correct `@property (strong, nonatomic) IBOutlet FarbView *farbview;` in FarbViewController.h file. And the last connect farbview in Interface Builder with its outlet. To make sure that object is not NULL just log it with function `NSLog(@" self.farbview = %@", self.farbview);` and you will see its value in output. Add this line before `self.farbView.delegate = self` to be sure that it is not NULL. – Vadim A. Sep 03 '13 at 12:24
  • Ohh man, bad mistake! Now it is working thanks for your time and help :) – user2738907 Sep 03 '13 at 13:42