如何在 UIViewController 中访问由 UIView 的子类创建的对象
How to access an object created by a subclass of UIView in a UIViewController
我的视图控制器中有一个 UIView 的子类,它通过手指触摸绘制 UIBezierPath:
#import "LinearInterpView.h"
@implementation LinearInterpView
{
UIBezierPath *path;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
[self setMultipleTouchEnabled:NO];
[self setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:1.0]];
path = [UIBezierPath bezierPath];
[path setLineWidth:2.0];
// Add a clear button
UIButton *clearButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 10.0, 80.0, 40.0)];
[clearButton setTitle:@"Clear" forState:UIControlStateNormal];
[clearButton setBackgroundColor:[UIColor lightGrayColor]];
[clearButton addTarget:self action:@selector(clearSandBox) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:clearButton];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[[UIColor darkGrayColor] setStroke];
[path stroke];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p];
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
@end
这里一切正常,但我需要在我的视图控制器中访问 finger 生成的路径并分析它。当我调试代码时,我可以在视图控制器中的 UIView 中看到变量 path
,但我无法以编程方式访问它。有什么方法可以访问子类创建的对象吗?
要访问 viewController
的路径,您必须将其定义为 Public 变量并通过 class.
外部访问它
将您的 : UIBezierPath *path;
定义到 @interface
文件中并将其访问到 ViewController
@interface LinearInterpView
{
UIBezierPath *path;
}
喜欢 :
LinearInterpView *viewLinner = [LinearInterpView alloc]initwithframe:<yourFrame>];
//By This line you can access path into your view controller.
viewLinner.path
您也可以创建该视图的 IBOutlet 并像上面一样访问。
谢谢。
我的视图控制器中有一个 UIView 的子类,它通过手指触摸绘制 UIBezierPath:
#import "LinearInterpView.h"
@implementation LinearInterpView
{
UIBezierPath *path;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
[self setMultipleTouchEnabled:NO];
[self setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:1.0]];
path = [UIBezierPath bezierPath];
[path setLineWidth:2.0];
// Add a clear button
UIButton *clearButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 10.0, 80.0, 40.0)];
[clearButton setTitle:@"Clear" forState:UIControlStateNormal];
[clearButton setBackgroundColor:[UIColor lightGrayColor]];
[clearButton addTarget:self action:@selector(clearSandBox) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:clearButton];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[[UIColor darkGrayColor] setStroke];
[path stroke];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p];
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
@end
这里一切正常,但我需要在我的视图控制器中访问 finger 生成的路径并分析它。当我调试代码时,我可以在视图控制器中的 UIView 中看到变量 path
,但我无法以编程方式访问它。有什么方法可以访问子类创建的对象吗?
要访问 viewController
的路径,您必须将其定义为 Public 变量并通过 class.
将您的 : UIBezierPath *path;
定义到 @interface
文件中并将其访问到 ViewController
@interface LinearInterpView
{
UIBezierPath *path;
}
喜欢 :
LinearInterpView *viewLinner = [LinearInterpView alloc]initwithframe:<yourFrame>];
//By This line you can access path into your view controller.
viewLinner.path
您也可以创建该视图的 IBOutlet 并像上面一样访问。
谢谢。