Delegate method is never called - ios

first. here is my code.
paintingView.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#protocol PaintingViewDelegate
-(void)makeLine:(CGPoint) touch;
#end
//CLASS INTERFACES:
#interface PaintingView : UIView
{
id <PaintingViewDelegate> delegate;
#private
CGPoint location;
CGPoint previousLocation;
Boolean firstTouch;
}
#property(nonatomic, readwrite) CGPoint location;
#property(nonatomic, readwrite) CGPoint previousLocation;
#property(nonatomic, assign) id <PaintingViewDelegate> delegate;
#end
paintingView.m
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>
#import "PaintingView.h"
//CLASS IMPLEMENTATIONS:
#implementation PaintingView
#synthesize location;
#synthesize previousLocation;
#synthesize delegate;
// Implement this to override the default layer class (which is [CALayer class]).
// We do this so that our view will be backed by a layer that is capable of OpenGL ES rendering.
+ (Class) layerClass
{
return [CAEAGLLayer class];
}
// Handles the start of a touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect bounds = [self bounds];
UITouch* touch = [[event touchesForView:self] anyObject];
firstTouch = YES;
// Convert touch point from UIView referential to OpenGL one (upside-down flip)
location = [touch locationInView:self];
location.y = bounds.size.height - location.y;
[[self delegate]makeLine:location];
}
...
#end
viewController.h
#import "PaintingView.h"
#interface GMViewController : UIViewController <AGSMapViewLayerDelegate, PaintingViewDelegate>
{
PaintingView *painting1;
}
...
#end
viewController.m
...
- (void)viewDidLoad
{
[super viewDidLoad];
painting1 = [[PaintingView alloc] init];
[painting1 setDelegate:self];
}
...
-(void)makeLine(CGPoint) touch
{
NSLog(#"TOUCH");
}
...
#end
my delegate method is never called. I have made my delegate method from the iOS-developer.net resources example. there are no errors during compiling.

Fixed it by setting the paintingView's delegate with the line
[painting setDelegate: self];
in my viewController class file.

Related

Using MKOverlayRenderer in iOS7 to display UIImage

I am trying to display a UIImage as a overlay for a map-based iPad application. I attempted to follow the 4 steps to setting a custom overlay defined in Apple's documentation: define the overlay data object, define the object renderer, implement the map view in map view delegate, and add overlay data object to to map view. After attempting to do all these, there is no overlay when I run the simulator. Any advice would be fantastic. My code is as follows
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "bjorkOverlay.h"
#import "bjorkOverlayRenderer.h"
#interface TUTViewController : UIViewController <MKMapViewDelegate>
{
CLLocationManager *locationManager;
CLLocation *location;
}
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#property(nonatomic) MKCoordinateRegion region;
#property (nonatomic, readonly) MKMapRect boundingMapRect;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, readonly) CLLocationCoordinate2D bjorkOverlayCenterPoint;
#property (nonatomic, readonly) MKMapRect bjorkOverlayBoundingMapRect;
#end
ViewController.m
#import "TUTViewController.h"
#import "bjorkOverlay.h"
#import "bjorkOverlayRenderer.h"
#import MapKit;
#interface TUTViewController () <MKMapViewDelegate>
#end
#implementation TUTViewController
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
_mapView.showsUserLocation = YES;
_mapView.delegate = self;
CLLocationCoordinate2D lowerLeftCoord = CLLocationCoordinate2DMake( 45.022943 ,-87.146606);
MKMapPoint lowerLeft = MKMapPointForCoordinate(lowerLeftCoord);
CLLocationCoordinate2D upperRightCoord = CLLocationCoordinate2DMake( 45.048513, -87.124765);
MKMapPoint upperRight = MKMapPointForCoordinate(upperRightCoord);
MKMapRect mapRect = MKMapRectMake(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, lowerLeft.y - upperRight.y);
[self.mapView setVisibleMapRect:mapRect animated:YES];
bjorkOverlay * mapOverlay = [[bjorkOverlay alloc] init];
[_mapView addOverlay:mapOverlay];
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay: (id<MKOverlay>)overlay {
MKOverlayRenderer *mapOverlay = [[MKOverlayRenderer alloc] init];
return mapOverlay;
}
// Do any additional setup after loading the view, typically from a nib.
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
bjorkOverlay.h
#import <Foundation/Foundation.h>
#import MapKit;
#interface bjorkOverlay : NSObject <MKOverlay>
+ (id)overlayWithCoordinate:(CLLocationCoordinate2D)coordinate;
#property (nonatomic, assign) CLLocationCoordinate2D bjorkOverlayCenterPoint;
#property (nonatomic) MKMapRect bjorkOverlayBoundingMapRect;
#end
bjorkOverlay.m
#import "bjorkOverlay.h"
#import MapKit;
#implementation bjorkOverlay
+ (id)overlayWithCoordinate:(CLLocationCoordinate2D)coordinate radius:(CLLocationDistance)radius{
bjorkOverlay* overlay = [[bjorkOverlay alloc] init];
overlay.coordinate = coordinate;
return overlay;
}
-(MKMapRect)boundingMapRect{
CLLocationCoordinate2D lowerLeftCoord = CLLocationCoordinate2DMake( 45.022943 ,-87.146606);
MKMapPoint lowerLeft = MKMapPointForCoordinate(lowerLeftCoord);
CLLocationCoordinate2D upperRightCoord = CLLocationCoordinate2DMake( 45.048513, -87.124765);
MKMapPoint upperRight = MKMapPointForCoordinate(upperRightCoord);
MKMapRect mapRect = MKMapRectMake(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, lowerLeft.y - upperRight.y);
return mapRect;
}
- (CLLocationCoordinate2D)bjorkOverlayCenterPoint
{
CLLocationCoordinate2D coord1 = {
45.035728,-87.1356855
};
return coord1;
}
#end
bjorkOverlayRenderer.h
#import <MapKit/MapKit.h>
#interface bjorkOverlayRenderer : MKOverlayRenderer
#end
bjorkOverlayRender.m
#import "bjorkOverlayRenderer.h"
#implementation bjorkOverlayRenderer
- (void)drawMapRect:(MKMapRect)mapRect
zoomScale:(MKZoomScale)zoomScale
inContext:(CGContextRef)ctx
{
UIImage *image = [UIImage imageNamed:#"overlay.png"];
CGImageRef imageReference = image.CGImage;
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
CGRect clipRect = [self rectForMapRect:mapRect];
CGContextAddRect(ctx, clipRect);
CGContextClip(ctx);
CGContextDrawImage(ctx, theRect, imageReference);
}
#end
Any advice would be great or if you have seen sample code for MKOverlayRenderer I would appreciate a link. Thanks

Trouble Parsing/Passing XML location data into map kit xCode 5

Following this tutorial on how to display XML latitude and longitude data as pins on iOS map kit:
http://highoncoding.com/Articles/805_Consuming_XML_Feed_and_Displaying_Public_Information_on_the_MapView_Control.aspx
The sample code provided compiles correctly and displays pins all across the united states. However when I tried to "port" the .xib into my app It pulls up my Mapview and the current users location, but it won't drop any pins/parse the data?
I'm on Day 2 now, its a bit discouraging.
Heres my .m and .h
EleventhViewController.h
// SlideMenu
//
// Created by Kyle Begeman on 1/13/13.
// Copyright (c) 2013 Indee Box LLC. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
//#interface EleventhViewController : NSObject <UIApplicationDelegate,MKMapViewDelegate,CLLocationManagerDelegate> {
#interface EleventhViewController : UIViewController <MKMapViewDelegate,CLLocationManagerDelegate,UIApplicationDelegate>
{
IBOutlet MKMapView *mapView;
NSMutableArray *greenCities;
CLLocationManager *locationManager;
}
//-(void) MKMapViewDelegate;
//-(void) CLLocationManagerDelegate;
//#property (nonatomic, weak) id<UIApplicationDelegate> delegate;
//#property (nonatomic, weak) id<MKMapViewDelegate> delegate;
//#property (nonatomic, weak) id<CLLocationManagerDelegate> delegate;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic,retain) IBOutlet MKMapView *mapView;
#property (nonatomic,retain) NSMutableArray *greenCities;
#property (strong, nonatomic) UIButton *menuBtn;
#property (strong, nonatomic) UIButton *searchBtn;
#end
Heres the .m
//
// EleventhViewController.m
// SlideMenu
//
// Created by Kyle Begeman on 1/13/13.
// Copyright (c) 2013 Indee Box LLC. All rights reserved.
//
#import "EleventhViewController.h"
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
#import "GreenCitiesAppDelegate.h"
#import "GreenCitiesService.h"
#import "GreenCityAnnotation.h"
#import "GreenCityAnnotationView.h"
#import "GreenCity.h"
#interface EleventhViewController ()
//#interface EleventhViewController : UIViewController <MKMapViewDelegate>
#end
#implementation EleventhViewController
#synthesize window=_window,mapView,greenCities;
#synthesize menuBtn;
#synthesize searchBtn;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.mapView.delegate = self;
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.mapView setShowsUserLocation:YES];
GreenCitiesService *greenService = [[GreenCitiesService alloc] init];
self.greenCities = [greenService getGreenCities];
[self.window makeKeyAndVisible];
return YES;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.window.layer.shadowOpacity = 0.75f;
self.window.layer.shadowRadius = 10.0f;
self.window.layer.shadowColor = [UIColor blackColor].CGColor;
if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]]) {
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Menu"];
}
self.menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
menuBtn.frame = CGRectMake(9, 23, 40, 30);
[menuBtn setBackgroundImage:[UIImage imageNamed:#"menuButton.png"] forState:UIControlStateNormal];
[menuBtn addTarget:self action:#selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:self.menuBtn];
//Top Main Menu Search Button
self.searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
searchBtn.frame = CGRectMake(275, 25, 40, 30);
[searchBtn setBackgroundImage:[UIImage imageNamed:#"searchButton.png"] forState:UIControlStateNormal];
[searchBtn addTarget:self action:#selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:self.searchBtn];
}
- (void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(#"didUpdateUserLocation fired!");
CLLocationCoordinate2D maxCoord = {-90.0f,-180.0f};
CLLocationCoordinate2D minCoord = {90.0f, 180.0f};
for(int i = 0; i<=[self.greenCities count] - 1;i++)
{
GreenCity *gCity = (GreenCity *) [self.greenCities objectAtIndex:i];
CLLocationCoordinate2D newCoord = { gCity.latitude, gCity.longitude };
if(gCity.longitude > maxCoord.longitude)
{
maxCoord.longitude = gCity.longitude;
}
if(gCity.latitude > maxCoord.latitude)
{
maxCoord.latitude = gCity.latitude;
}
if(gCity.longitude < minCoord.longitude)
{
minCoord.longitude = gCity.longitude;
}
if(gCity.latitude < minCoord.latitude)
{
minCoord.latitude = gCity.latitude;
}
GreenCityAnnotation *annotation = [[GreenCityAnnotation alloc] initWithCoordinate:newCoord title:gCity.name subTitle:gCity.rank];
[mv addAnnotation:annotation];
// [annotation release];
}
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0;
region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0;
// calculate the span
region.span.longitudeDelta = maxCoord.longitude - minCoord.longitude;
region.span.latitudeDelta = maxCoord.latitude - minCoord.latitude;
[self.mapView setRegion:region animated:YES];
}
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)revealMenu:(id)sender
{
[self.slidingViewController anchorTopViewTo:ECRight];
}
#end
My Project is pretty simple, uses a lot of free source code so feel free to download what I've made up to this point, I'm pretty sure it would make a good template/starting base for a lot of you:
https://www.dropbox.com/s/8xxx08zyqpr9i8v/CDF.zip
The method didFinishLauching with options should only be used in the app delegate. You need to move the code from here into viewDidLoad and delete the didFinishLaunching method. Also delete the reference you have to the app delegate from the storyboard xib.
You really want to then set breakpoints in the app and work out where the data load is going wrong.
That fixed it! I moved every piece of executed code under the viewDidLoad! I feel like a noob (which I am) I would up vote you but I don't have enough rep yet. However I keep my connections to the app delegate I imported from the greencities .zip source. Thanks so much. This has however created a new bug in that I can't access my slider menu...fix one problem get another...I really appreciate the direction

Drag UIImageView out of ScrollView

I have a RootViewController with a UIScrollViewController (boardScrollView).
This boardScrollView has a UIImageView as subview (boardImage) to create the board. I can zoom in and out and scroll on the boardImage within the boardScrollView. Works great!
Now I want to drag & drop other UIImageViews into the boardScrollImage within the boardScrollView and also OUT of the boardScrollView.
For these other UIImageViews (Tiles) I have created a subclass of the UIImageView class (TileViewClass).
I have the drag&drop working to drop the Tile INTO the boardScrollView/boardImage and also drag&drop INSIDE the boardScrollView/boardImage but I can not get the drag&drop to OUTSIDE the boardScrollView working..
I think this is because I can not access the views in the rootviewcontroller from the subclass.
Maybe it is even better to place the Tile back to the topview (window) in touchesBegan, and so the determination of the drop-position is always done from the same view.
But I don't know how this can be done...
I have tried [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self.dragObject]; in the touchesBegan method, but this does not do the trick....
Maybe I am missing a removeFromSuperView somewhere?
Anyone any idea how I can get the drag&drop working?
RootViewController.h:
#interface RootViewController : UIViewController <UIScrollViewDelegate>
#property (nonatomic, strong) IBOutlet UIScrollView *boardScrollView;
#property (nonatomic, strong) UIImageView *dragObject;
#property (nonatomic, assign) CGPoint touchOffset;
#property (nonatomic, assign) CGPoint homePosition;
#property (nonatomic, strong) UIImageView *boardImage;
#end
RootViewController.m:
#implementation RootViewController
#synthesize boardScrollView;
#synthesize dragObject;
#synthesize touchOffset;
#synthesize homePosition;
#synthesize boardImage;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImage *image = [UIImage imageNamed:#"greyblue_numbered_15x15_900x900.png"];
self.boardImage = [[UIImageView alloc] initWithImage:image];
self.boardImage.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.boardScrollView addSubview:self.boardImage];
self.boardImage.userInteractionEnabled = YES;
self.boardScrollView.contentSize = image.size;
self.boardScrollView.canCancelContentTouches = NO;
self.boardScrollView.userInteractionEnabled = YES;
self.boardScrollView.clipsToBounds = YES;
}
TileImageView.h:
#import <UIKit/UIKit.h>
#interface TileImageView : UIImageView
#property (nonatomic, strong) UIImageView *dragObject;
#property (nonatomic, assign) CGPoint touchOffset;
#end
TileImageView.m:
#import "TileImageView.h"
#implementation TileImageView
#synthesize dragObject;
#synthesize touchOffset;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.exclusiveTouch = YES;
self.userInteractionEnabled = YES;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 1) {
// one finger
CGPoint touchPoint = [[touches anyObject] locationInView:self.superview];
for (UIImageView *iView in self.superview.subviews) {
if ([iView isMemberOfClass:[TileImageView class]]) {
if (touchPoint.x > iView.frame.origin.x &&
touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
touchPoint.y > iView.frame.origin.y &&
touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
{
self.dragObject = iView;
self.touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
touchPoint.y - iView.frame.origin.y);
[self.superview bringSubviewToFront:self];
}
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.superview];
CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
touchPoint.y - touchOffset.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
self.dragObject.frame = newDragObjectFrame;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UIView *iView in self.superview.subviews) {
if ([iView isMemberOfClass:[UIScrollView class]])
{
CGPoint touchPointScreen = [[touches anyObject] locationInView:[UIApplication sharedApplication].keyWindow];
if (touchPointScreen.x > iView.frame.origin.x &&
touchPointScreen.x < iView.frame.origin.x + iView.frame.size.width &&
touchPointScreen.y > iView.frame.origin.y &&
touchPointScreen.y < iView.frame.origin.y + iView.frame.size.height)
{
for (UIView *iView2 in iView.subviews) {
[iView2 addSubview:self.dragObject];
CGPoint touchPointImage = [[touches anyObject] locationInView:iView2];
self.dragObject.frame = CGRectMake(touchPointImage.x - touchOffset.x,
touchPointImage.y - touchOffset.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
}
}
self.dragObject = nil;
}
}
Basically you'r catching all touch events on the tileimageview and moving its origin CGPoint around.
If a touch event ended, your cycling though all subviews of your tileviews superview.
If any1 of this is a UIScrollview, you try to locate if the touchpoint matches its frame.
So basically you just checking the frame of your uiscrollview. Therefore your touchpoint cannot be outside it.
Solution: you have to check the view in which you want to drop the tileimageview! you can assume that its a sibling of your uiscrollview(what i assume).
Other way, which i would recommend: Create a View - dropgroundview, in which you catch the touches(=> implement touchesend)
there you can drop your views around and check for subclasses of e.g. dropviews.
Solution: I have delegated the touch methodes to the RootViewController. There, I have access to all subviews and most important: the highest view.
Here, I say:
[self.view bringSubviewToFront:self.tmpDragObject];
And then it works!

How to call a UIView method in my UIViewController?

I have a method to create a rectangle in my custom uiview and I want to call it in my UIViewController.I am trying to use UITapGestureRecognizer in my UIViewController and check if a point in the rectangle is called.
rectangle in my custom uiview ( myView.m )
- (CGRect) myRect
{
return CGRectMake(10, 10, self.bounds.size.height - 20, self.bounds.size.height - 20);
}
UITapGestureRecognizer method in my MyViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self.PlayDraw setNeedsDisplay];
}
- (IBAction)handleTap:(UITapGestureRecognizer *)sender {
CGPoint point = [sender locationInView:self.view];
if(CGRectContainsPoint([self.PlayDraw myRect],point)) {
NSLog(#"touched");
}
and I get an error for [self.PlayDraw myRect] that says
No visible #interface for 'MyViewController' declares the
selector 'myRect'
and here is my MyViewController.h
#interface MyViewController : UIViewController
{
}
- (IBAction)handleTap:(UITapGestureRecognizer *)sender;
#property (strong, nonatomic) IBOutlet myView *PlayDraw;
#end
In your myView.h file add:
- (CGRect) myRect;
You have to expose this method to other classes.

Communication between A ViewController and its View

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
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>

Resources