2017-03-18 66 views
0

我的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,它存在於我的視圖控制器中,但我無法以編程方式訪問它。有什麼方法可以訪問由子類創建的對象嗎?

+0

是不是隻是將視圖與LinearInterpView連線以創建IB插座的事情? –

+0

其實我已經有了UIView的IB插座。我需要訪問UIView內存在的UIBezierPath。 –

+0

這是不可能的,我想。您可以做的最接近的事情是訪問包含具有某些變量的路徑對象的類,以便您可以用用戶定義的值重現它。 –

回答

0

要訪問到您的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

相關問題