2012-04-30 4 views
0

スライダの値に応じて上下に動くグラフを描画しようとしています。 私のグラフは、カスタムクラスGraphViewに属するビューで描画されています。 プロジェクトのViewControllerは1つあり、スライダはmoveLineメソッドを呼び出します。 これは私がするように設定されたプロパティendXPointあります endXPoint = mySlider.valueXcodeカスタムクラスからスライダの値を取得する方法

私の問題は、私は私のGraphViewののdrawRectメソッドの内部からこの値を参照する方法がわからないということです。

ViewControllerでGraphViewへの参照を作成してそこにプロパティを設定しようとしましたが、機能しません。
GraphView * myGraphView =(GraphView *)self.view;
myGraphView.endXPoint = mySlider.value;

答えて

0

あなたはそのようなGraphViewクラスのプロパティを設定する必要があります。あなたのようGraphView変数を設定

@interface GraphView : UIView 
@property float endXPoint; 

その後のViewControllerから:

[myGraphView setendXPoint: [mySlider value]]; 
[myGraphView setNeedsDisplay]; 

最後の行を更新するためにGraphViewを頼みますView、drawRectメソッドを呼び出します。 drawRectメソッドでは、endXPointはクラスプロパティであるため直接使用できます。

これが正しいバージョンである:私はそれが私のログを与えて怖い

//ViewController.h 

#import <UIKit/UIKit.h> 
#import "GraphView.h" //import headers in the header file 

@interface ViewController : UIViewController 

@property (strong, nonatomic) IBOutlet UISlider *mySlider; 
@property (strong, nonatomic) IBOutlet UILabel *myLabel; 
@property (strong, nonatomic) IBOutlet GraphView *myGraphView; //Connect this with the IB 

- (IBAction)moveLine:(id)sender; 
- (IBAction)setLabelText:(id)sender; 
@end 

//ViewController.m 

#import "ViewController.h" 

@implementation ViewController 
@synthesize mySlider; 
@synthesize myLabel; 
@synthesize myGraphView; 



- (IBAction)moveLine:(id)sender { 
    [myGraphView setendXPoint:[mySlider value]]; 
    [myGraphView setNeedsDisplay]; 
} 

@end 

//GraphView.h 

#import <UIKit/UIKit.h> 

@interface GraphView : UIView 
@property float endXPoint; 


@end 

//GraphView.m 

#import "GraphView.h" 

@implementation GraphView 
@synthesize endXPoint; 


- (void)drawRect:(CGRect)rect 
{ 

    CGContextRef ctx = UIGraphicsGetCurrentContext(); //get the graphics context 
    CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1); 
    CGContextMoveToPoint(ctx, 0, 0); 
    //add a line from 0,0 to the point 100,100; 
    CGContextAddLineToPoint(ctx, endXPoint,100); 
    //"stroke" the path 
    CGContextStrokePath(ctx); 
} 


@end 
+0

: - [UIViewのsetEndXPointは:]:インスタンス0x6a0faf0 –

+0

に送信された認識されていないセレクタは、あなたが全体のコードを投稿してくださいことはできますか? –

+0

私はコードを投稿する方法を読む必要があります。このコメントボックスはそれを保持しません。 –

関連する問題