【问题标题】:Communication between A ViewController and its ViewViewController 和它的 View 之间的通信
【发布时间】:2013-09-02 07:35:37
【问题描述】:

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

在这些步骤之后,UIColor 应该被发送到UIViewController。 我试图将UIColor 变量添加到ViewController(委托和实例化),但没有任何效果。

代码如下。

更新:尝试了一个答案,但没有奏效。更新了这段代码。

这里是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

【问题讨论】:

    标签: ios view viewcontroller pass-data


    【解决方案1】:

    我不建议在这里使用 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 触摸开始处理程序:

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

    【讨论】:

    • 好的,我试过了,但它也不起作用。发送代码作为答案:
    • 好的。可能是 touchesBegan 方法不起作用?如果您的视图交互关闭,则可能会发生这种情况。在界面生成器中检查标志“用户交互已启用”。它应该开启。此外,如果您通过 Interface Builder 添加 farbView,请检查是否为您的 farbView 选择自定义类(它位于 Interface Builder 右侧的“身份检查器”上)。
    • 我检查了它,“用户交互启用”已打开,并且选择了自定义类“FarbView”。我也尝试在 Xcode 4 上使用它,但也没有用。所以我想这可能是一个项目问题,在这种情况下我打开了一个新项目,没有工作。还有什么我能做的想法吗?
    • 你用debug了吗?程序是否进入 touchesBegan 方法?还要确保 FarbView 的所有父视图的“用户交互已启用”为“开启”
    • 当你使self.farbView.delegate = selfself.farbView 不是NULL[delegate respondsToSelector:@selector(receiveNewColor:)] 是否返回 YES?在 touchesBegan 上,委托也不应该为 NULL。
    猜你喜欢
    • 2015-05-02
    • 2012-06-02
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 2019-02-06
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多