我有應用程序來創建圖形我已經創建了一個基於視圖的應用程序,然後添加用於創建圖形的代碼,但它不顯示圖形。如果使用相同的代碼來創建一個單獨的UIView,然後它工作等最好不要帶有id的SimpleView Based應用程序initWithFrame不起作用
#import <UIKit/UIKit.h>
#import "ECGraph.h"
#import "ECGraphItem.h"
@class GraphsViewController;
@interface Display : UIView {
NSArray *percentages;
int myY;
ECGraph *graph;
ECGraphItem *item1;
ECGraphItem *item2;
}
@property(nonatomic,retain)NSArray*percentages;
-(void) setPercentageArray:(NSArray*) array;
@end
#import "Display.h"
#import "ECGraph.h"
@implementation Display
@synthesize percentages;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef _context = UIGraphicsGetCurrentContext();
graph = [[ECGraph alloc] initWithFrame:CGRectMake(500,-320,320, 200) withContext:_context isPortrait:NO];
item1 = [[ECGraphItem alloc] init];
item2 = [[ECGraphItem alloc] init];
/*
ECGraphItem *item1 = [[ECGraphItem alloc] init];
ECGraphItem *item2 = [[ECGraphItem alloc] init];*/
item1.isPercentage = YES;
item1.yValue=myY;
item1.width = 35;
item1.name = @"item1";
item2.isPercentage = YES;
item2.yValue =17;
item2.width = 35;
item2.name = @"item2";
[graph setXaxisTitle:@"name"];
[graph setYaxisTitle:@"Percentage"];
[graph setGraphicTitle:@"Histogram"];
[graph setDelegate:self];
[graph setBackgroundColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1]];
NSArray *items = [[NSArray alloc] initWithObjects:item1,item2,nil];
[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];
}
我加入在GraphsViewController這個觀點,但它不顯示任何
//實現viewDidLoad中加載視圖後做額外的設置,通常來自筆尖。
- (void)viewDidLoad {
[super viewDidLoad];
[self createGraph];
percentages = [NSArray arrayWithObjects:@"80",@"17", nil];
display = [[Display alloc] init];
[self.view addSubview:display];
[display setPercentageArray:percentages];
}
你在哪裏添加代碼的第二塊?在TestingViewController中?你確定對CGRectMake的這個調用?那個參數是x,y,寬度和高度。你有一個-320的位置和200的高度。 – Nate
@Nate我認爲亞歷山大說的是對的,我們需要在ViewController中爲單獨的UIView工作,但是當我補充說它不適用於我 – user1392981