gpt4 book ai didi

ios - ViewController 和它的 View 之间的通信

转载 作者:行者123 更新时间:2023-12-01 19:08:03 25 4
gpt4 key购买 nike

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

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

代码如下。

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

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

最佳答案

我不建议在这里使用 NSNotificationCenter。

从 child 接收回调的最佳方式是委托(delegate)模式。

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>

关于ios - ViewController 和它的 View 之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18567873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com