如何访问一个UIViewController
问题描述:
我的UIView的子类在我的视图控制器,通过手指触摸绘制UIBezierPath通过的UIView的一个子类创建的对象:如何访问一个UIViewController
#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
一切工作就好了这里,但我需要访问我的视图控制器中手指生成的路径并对其进行分析。当我调试代码时,我可以在UIView中看到变量path
,它存在于我的视图控制器中,但我无法以编程方式访问它。有什么方法可以访问由子类创建的对象吗?
答
要访问到您的viewController
的路径,您必须将其定义为公共变量并通过该类以外的地方访问它。
定义您: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中与上面相同。
谢谢。
+0
以上代码可以帮助您@Bakak? – CodeChanger
是不是只是将视图与LinearInterpView连线以创建IB插座的事情? –
其实我已经有了UIView的IB插座。我需要访问UIView内存在的UIBezierPath。 –
这是不可能的,我想。您可以做的最接近的事情是访问包含具有某些变量的路径对象的类,以便您可以用用户定义的值重现它。 –