J'ai une sous-classe de UIView dans mon contrôleur de vue qui tire une UIBezierPath par le toucher du doigt:Comment accéder à un objet créé par une sous-classe de UIView dans un 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
Tout fonctionne très bien, mais je besoin d'accéder au chemin généré par le doigt dans mon contrôleur de vue et de l'analyser. Quand je débogue le code, je peux voir la variable path
dans le UIView qui est présent dans mon contrôleur de vue, mais je ne peux pas y accéder par programmation. Est-il possible d'accéder à un objet créé par une sous-classe?
N'est-il pas simplement question de connecter une vue avec LinearInterpView pour créer une prise IB Outlet? –
Non, en fait, j'ai déjà le point de vente IB à l'UIView. J'ai besoin d'accéder à UIBezierPath qui est présent dans l'UIView. –
Ce n'est pas possible, je pense. La chose la plus proche que vous pouvez faire est d'accéder à une classe contenant un objet path avec certaines variables afin que vous puissiez le reproduire avec des valeurs définies par l'utilisateur. –