Communication between A ViewController and its View

967 views Asked by At

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
1

There are 1 answers

8
Vadim A. On BEST ANSWER

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>