ViewController与其视图之间的通信

时间:2013-09-02 07:35:37

标签: ios view viewcontroller pass-data

我创建了两个类1UIViewControllerClass及其1UIViewClass(这是ViewController的视图)。在我的UIViewClass我有两种方法,其中一种是touchesBegan,它获取了触摸点的位置。仅在View上运行的第二个Methode获取位置信息并获取该位置的颜色。第二个Methode返回UIColor

完成这些步骤后,UIColor应发送至UIViewController。 我试图将UIColor变量传递给ViewController(委托和Instanzing),但没有任何效果。

代码如下。

更新:尝试了一个答案但是没有用。更新了此代码。

以下是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

1 个答案:

答案 0 :(得分: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 touch开始处理程序:

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

在ViewController类中添加方法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;

在这里你会有警告。只需将“FarbDelegate”添加到@interface行:

@interface FarbViewController:UIViewController<FarbDelegate>