我想在用戶界面上的現有視圖中繪製條形圖。單條高點在用戶輸入後計算並存儲在一個數組中。數組應該傳遞給我自定義的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
什麼錯誤?什麼不行? – bryanmac
我不會推薦具有與屬性相同名稱的實例變量(ivars)。 – ColinE
@ bryanmac:謝謝。在GraphView.m中使用'NSNumber * balkenHoehe = [balkenArray objectAtIndex:i];'這一行不會停止,並且我會在日誌窗口中獲得數千個有線筆記。只要我把'balkenHoehe'作爲變量的高度,在嘗試構建時出現錯誤「將'NSNumber * __ strong'傳遞給不兼容類型'CGFloat'(又名'double')的參數。 – JFS