访问视图控制器中自定义视图的方法

时间:2012-11-17 14:00:44

标签: objective-c ios

我是目标C的新人。在这里,我想问一个简单的问题 我在storyboard中创建了一个视图控制器,并使用UIView的子类自定义了它的视图 但是,我不知道如何在视图控制器中调用视图的方法。有人可以帮忙吗?我想要做的就是从drawLine:pointStore

致电ChartViewController.m中的chartView.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];
}

chartView.h

@interface chartView : UIView

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

@end

chartView.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];
    }
}

2 个答案:

答案 0 :(得分:0)

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

如何执行此操作 - 取决于您选择创建视图的方式:在Interface Builder中或动态地,在代码中。

答案 1 :(得分:0)

ChartViewController.h

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

@interface ChartViewController : UIViewController

@property (string, nonatomic) chartView *chart_view;

@end

chartView.m

- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    ...
    // Don't forget to alloc and init
    self.chart_view = [[chartView alloc] init];
    [self.chart_view drawLine:pointStore];
}