2017-03-18 3 views
0

のUIViewControllerでのUIViewのサブクラスにより作成されたオブジェクトにアクセスする方法:ここ指のタッチでUIBezierPathを描き、私は私のビューコントローラでのUIViewのサブクラスを持って

#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 

すべてがうまく動作しますが、Iビューコントローラで指で生成されたパスにアクセスして分析する必要があります。私はコードをデバッグするとき、私は、ビューコントローラに存在するUIViewの変数pathを見ることができますが、私はそれをプログラム的にアクセスすることはできません。サブクラスによって作成されたオブジェクトにアクセスする方法はありますか?

+0

LinearInterpViewでビューを配線接続して、IB Outletを作成するだけでは問題ありませんか? –

+0

実際には、私はすでにIBViewをUIViewに持っていません。 UIViewの内部にあるUIBezierPathにアクセスする必要があります。 –

+0

これは不可能だと思います。あなたができる最も近いことは、いくつかの変数を持つパスオブジェクトを含むクラスにアクセスして、ユーザー定義の値でそれを再現できるようにすることです。 –

答えて

0

viewControllerへのアクセスには、それをパブリック変数として定義し、そのクラスの外部からアクセスする必要があります。それは次のようにViewController

@interface LinearInterpView 
{ 
    UIBezierPath *path; 
} 

UIBezierPath *path;@interfaceにファイルやアクセス:また、あなたがそのビューと上記と同じアクセスのIBOutletを作成することができます

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

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

はあなたを定義します。

ありがとうございました。

+0

は上記のコードは@Babakに役立ちますか? – CodeChanger

関連する問題