2012-12-06 56 views
0

我想在用戶界面上的現有視圖中繪製條形圖。單條高點在用戶輸入後計算並存儲在一個數組中。數組應該傳遞給我自定義的GraphView類,最後在一個按鈕被刷新以繪製條形圖之後。但它不起作用。我不知道我是否正確傳遞數組(如何檢查它?)以及如何通過我的drawrect方法訪問它以設置單欄高點?我儘快把NSNumber *balkenHoehe = [balkenArray objectAtIndex:i];作爲直接法,balkenHoehe作爲直接法,我得到了錯誤。我需要做什麼?謝謝!傳遞數組以繪製條形圖

這裏是我的代碼:

我AppDelegate.h

#import <Cocoa/Cocoa.h> 

@interface AppDelegate : NSObject <NSApplicationDelegate> 

@property (assign) IBOutlet NSWindow *window; 
@property (weak) IBOutlet NSTextField *eingabeNennmass; 
@property (weak) IBOutlet NSTextField *eingabeToleranzOben; 
@property (weak) IBOutlet NSTextField *eingabeToleranzUnten; 
@property (weak) IBOutlet NSTextField *eingabeAnzahlWerte; 
@property (weak) IBOutlet NSTextField *ausgabeMittelwert; 
@property (weak) IBOutlet NSTextField *ausgabeToleranz 
@property (weak, nonatomic) IBOutlet GraphView *ausgabeGraph; 

- (IBAction)pushSimulation:(NSButton *)sender; 

@end 

我AppDelegate.m縮短

#import "GraphView.h" 
#import "AppDelegate.h" 

implementation AppDelegate 
....//couple calculating code and building the array klassenArray//... 

    [klassenArray addObject:[NSNumber numberWithDouble: n]]; 
....//more code//... 

[_ausgabeGraph setBalkenArray:klassenArray]; (connected with the GraphView view on the user interface) 

....//more code//... 

我GraphView.h

#import <Cocoa/Cocoa.h> 

@interface GraphView : NSView 
{ 

NSMutableArray *balkenArray; 

} 

- (NSMutableArray *) balkenArray; 
- (void) setBalkenArray:(NSMutableArray *)abalkenArray; 

@end 

我GraphView.m

#import "GraphView.h" 

@implementation GraphView 

//******************************* 
- (void) setBalkenArray:(NSMutableArray *)abalkenArray 
{ 
    balkenArray = abalkenArray; 
    [self setNeedsDisplay:YES]; 
} 

//******************************* 
- (NSMutableArray *)balkenArray 
{ 
    return balkenArray; 
} 

//******************************* 
- (id)initWithFrame:(NSRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

    } 

    return self; 
} 

//******************************* 
- (void)drawRect:(NSRect)dirtyRect 
{ 

    NSRect bounds = [self bounds]; 
    float fieldWith = bounds.size.width/12.0; 
    float fieldHeight = bounds.size.height/12.0; 

     //background 
    NSRect hintergrundRect = 
    hintergrundRect = NSMakeRect(bounds.origin.x, bounds.origin.y, 
            bounds.size.width, bounds.size.height); 
    [[NSColor grayColor] set]; 
    [NSBezierPath fillRect:hintergrundRect]; 

     //Diagram 

    for (int i = 1; i<=10; i++) { 
     NSNumber *balkenHoehe = [balkenArray objectAtIndex:i]; 
//  NSLog(@"rectnumber at index %d is %@", i, balkenHoehe); 

     NSRect aRect = 
     aRect = NSMakeRect (fieldWith*i, fieldHeight, fieldWith, balkenHoehe); //x, y, width, hight 
     // draw rect 
    [[NSColor whiteColor] set]; 
    [NSBezierPath fillRect:aRect]; 

     // draw border 
    [[NSColor blackColor] set]; 
    NSBezierPath *aPath = [NSBezierPath bezierPathWithRect:aRect]; 
    [aPath setLineWidth:1]; 
    [aPath stroke]; 
    } 
} 


@end 
+0

什麼錯誤?什麼不行? – bryanmac

+0

我不會推薦具有與屬性相同名稱的實例變量(ivars)。 – ColinE

+0

@ bryanmac:謝謝。在GraphView.m中使用'NSNumber * balkenHoehe = [balkenArray objectAtIndex:i];'這一行不會停止,並且我會在日誌窗口中獲得數千個有線筆記。只要我把'balkenHoehe'作爲變量的高度,在嘗試構建時出現錯誤「將'NSNumber * __ strong'傳遞給不兼容類型'CGFloat'(又名'double')的參數。 – JFS

回答

0

好,感謝我在這裏!好小子。 我用@property和GrapView.h

@interface GraphView : NSView 
{ 
NSMutableArray *balkenArray; 
} 
@property(nonatomic, retain)NSMutableArray *balkenArray; 
@property(readwrite, assign)double _maxKlasse; 
@end 

和GraphView.m

@synthesize balkenArray, _maxKlasse; 

在AppDelegate.m @systnesize事情陣列被髮送給視圖:

_ausgabeGraph.balkenArray=klassenArray; 
_ausgabeGraph._maxKlasse=maxKlasse; 
[_ausgabeGraph setNeedsDisplay:YES]; 

,最後在GraphView.m中使用該數組繪製條形圖:

... ...代碼

for (int i = 1; i<=10; i++) { 
    double _x = [(NSNumber*)[balkenArray objectAtIndex:i-1] doubleValue]; 

    //draw rect 
    NSRect aRect = 
    aRect = NSMakeRect (fieldWith * i, fieldHeight, fieldWith, (6*fieldHeight*_x/_maxKlasse)); //x, y, width, variable hight 
    [[NSColor whiteColor] set]; 
    [NSBezierPath fillRect:aRect]; 

...更多...代碼使用屬性和合成

感謝加布裏埃萊Petronella的提示。只要我明白它更容易使用。我仍然需要了解命名的約定。