2017-03-18 2 views
0

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?

+0

N'est-il pas simplement question de connecter une vue avec LinearInterpView pour créer une prise IB Outlet? –

+0

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. –

+0

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. –

Répondre

0

Pour accéder au chemin dans votre viewController, vous devez le définir en tant que variable publique et y accéder en dehors de cette classe.

Définir votre:UIBezierPath *path; dans @interface fichier et l'accès dans ViewController

@interface LinearInterpView 
{ 
    UIBezierPath *path; 
} 

comme:

LinearInterpView *viewLinner = [LinearInterpView alloc]initwithframe:<yourFrame>]; 

//By This line you can access path into your view controller. 
viewLinner.path 

vous pouvez également créer IBOutlet de ce point de vue et l'accès comme ci-dessus.

Merci.

+0

est au dessus du code vous aide @Babak? – CodeChanger