0

我是Objective C的新手。在这里我想问一个简单的问题。
我在 中创建了一个视图控制器storyboard,并使用UIView.
但是,我不知道如何在我的视图控制器中调用视图的方法。任何人都可以帮忙吗?我想做的drawLine:pointStore就是ChartViewController.mchartView.m

这是我的一些代码。
ChartViewController.h

#import <UIKit/UIKit.h>
@class chartView;

@interface ChartViewController : UIViewController{  
    chartView *chart_view;  
}  

ChartViewController.m

- (void)viewDidLoad  
{  
    [super viewDidLoad];  

    NSMutableDictionary *pointStore = [[NSMutableDictionary alloc]init];  

    NSNumber *initX;  
    NSNumber *initY;  
    NSMutableDictionary *variableSet = [[NSMutableDictionary alloc]init];
    for(initX = [NSNumber numberWithDouble:-10.0f];initX.floatValue<=10.0f;initX = [NSNumber numberWithDouble:(initX.floatValue+0.5f)] )
    {
        [variableSet setValue:initX forKey:@"x"];
        initY = [NSNumber numberWithDouble:[self.brain performOperation:equationOfChart withVariable:variableSet]];
        [pointStore setObject:initX forKey:initY];
    }
    [chart_view drawLine:pointStore];
}

图表视图.h

@interface chartView : UIView

@property (nonatomic, strong) NSString *equation;
-(void) getEquation:(NSString *)Equation;
-(void) drawLine:(NSMutableDictionary *)pointsStore;

@end

图表视图.m

-(void)drawLine:(NSMutableDictionary *)pointsStore{
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
    CGContextSetStrokeColor(c, red);
    CGContextBeginPath(c);
    CGContextStrokePath(c);
    for(NSNumber *ax = [NSNumber numberWithDouble:-10.0f];ax.floatValue<10.0f;){
        float ay = [[pointsStore objectForKey:ax]doubleValue];
        ax = [NSNumber numberWithDouble:(ax.floatValue+0.5f)];
        int by = [[pointsStore objectForKey:ax]doubleValue];
        [self.class line:ax.floatValue y:ay  x2:(ax.floatValue+0.5) y2:by];
    }
}
4

2 回答 2

0

您应该检查是否chart_view已创建并分配了实例。可能你在那个指针中没有零。

如何做到这一点 - 取决于您选择创建视图的方式:在 Interface Builder 中或在代码中动态。

于 2012-11-17T14:45:44.113 回答
0

ChartViewController.h

#import <UIKit/UIKit.h>
@class chartView;

@interface ChartViewController : UIViewController

@property (string, nonatomic) chartView *chart_view;

@end

图表视图.m

- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    ...
    // Don't forget to alloc and init
    self.chart_view = [[chartView alloc] init];
    [self.chart_view drawLine:pointStore];
}
于 2012-11-17T14:48:29.093 回答