2013-09-02 4 views
1

두 개의 클래스 1UIViewControllerClass과 그 1UIViewClass (어느 쪽이 ViewController의 뷰인가)을 작성했습니다. 내 UIViewClass에는 두 가지 방법이 있습니다. 그 중 하나는 touchesBegan이며, 이는 터치 된 지점의 위치를 ​​가져옵니다. 보기에서만 실행되는 두 번째 Methode는 위치 정보를 가져 와서이 위치에서보기에 색상을 가져옵니다. 두 번째 Methode는 UIColor과 함께 반환됩니다.ViewController와 뷰 사이의 통신

UIColorUIViewController으로 보내야합니다. 변수 ViewController (대리인 및 Instanzing)에 UIColor 변수를 가져 오려고했지만 아무 효과가 없습니다.

코드는 다음과 같습니다.

업데이트 : 한 가지 대답을 시도했지만 작동하지 않았습니다. 이 코드를 업데이트했습니다. 여기

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 

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

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

그리고 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 

답변

3

여기서 NSNotificationCenter는 사용하지 않는 것이 좋습니다.

자식에게서 콜백을받는 가장 좋은 방법은 위임 패턴입니다.

FarbView.h 파일 :

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


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

FarbView.m 터치 핸들러 시작 :

의 ViewController 클래스에서
//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); 
} 

방법 receiveNewColor의 선언을 추가

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

을하지 않으며, 다음 코드 줄을 viewDidLoad 메서드에 추가하는 것을 잊어 버리십시오.

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

경고가 표시됩니다. @interface 줄에 "FarbDelegate"를 추가하면됩니다.

@interface FarbViewController:UIViewController<FarbDelegate> 
+0

그래도 시도했지만 작동하지 않습니다. 코드를 답으로 보내십시오. – user2738907

+0

Ok. touches 일 수도 있습니다 .Began 메서드가 작동하지 않습니까? 보기 상호 작용이 해제 된 경우 발생할 수 있습니다. 인터페이스 빌더에서 "사용자 상호 작용 사용"플래그를 체크인하십시오. 그것은 켜져 있어야합니다. 또한 Interface Builder를 통해 farbView를 추가하는 경우, farbView (인터페이스 빌더의 오른쪽에있는 "ID 관리자")의 사용자 정의 클래스를 선택하십시오. –

+0

"사용자 상호 작용 사용"이 켜져 있고 사용자 지정 클래스 "FarbView"가 선택되어 있습니다. Xcode 4에서도 사용하려고했으나 작동하지 않았습니다. 그래서 아마도 그것이 프로젝트 이슈라고 생각했습니다.이 경우에는 새로운 프로젝트를 열었고 작동하지 않았습니다. 내가 할 수있는 다른 아이디어? – user2738907