将数组传递给绘制条形图

时间:2012-12-06 22:12:13

标签: objective-c arrays drawrect

我想在用户界面的现有视图中绘制条形图。单条高位在用户输入后计算并存储在数组中。在按下一个按钮以最终绘制条形图后,应该将数组传递给我的自定义GraphView类。但它不起作用。我不知道我是否正确传递数组(如何检查它?)以及如何使用我的drawrect方法访问它以设置单条高位?我把NSNumber *balkenHoehe = [balkenArray objectAtIndex:i];放在drawrect方法中,balkenHoehe作为rect-hight,我得到错误。我需要做什么?谢谢!

这是我的代码:

我的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

1 个答案:

答案 0 :(得分:0)

好的,谢谢你自己!好小子。 我在GrapView.h中使用了@property和@systnesize的东西

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

和GraphView.m

@synthesize balkenArray, _maxKlasse;
在AppDelegate.m中

将数组发送到视图:

_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];

...更多代码...

感谢Gabriele Petronella提供的使用属性和合成的提示。只要我明白它使用起来就容易多了。我仍然需要学习命名的约定。