替换另一个类中的变量值

时间:2012-04-05 23:02:02

标签: objective-c xcode

我上课了 * ColorViewController *的DrawRectangle

在ColorViewController.h中:

#import "DrawRectangle.h"
@interface ColorViewController : UIViewController {
DrawRectangle *DrawRectangle;
}

@property (nonatomic,retain) DrawRectangle *DrawRectangle;

在DrawRectangle.h中:

@interface DrawRectangle : UIView {
CGFloat redc;
}
@property (nonatomic) CGFloat redc;

我还将ViewController.xib中的DrawRectangle类添加到UIView之一。

我想从文件ColorViewController.m更改CGFloat redc的值 我是这样做的:

- (void)viewDidLoad
{
CGFloat myfloat = 1.0;
self.DrawRectangle.redc = myfloat;

我在DrawRectangle中使用'redc'调用NSLog,但每次都说0.0000000。

如何从ViewController替换'redc'的值?我不认为我需要使用NSNotification,如果我将DrawRectangle加载到ViewController中,必须有更简单的方法。

1 个答案:

答案 0 :(得分:2)

看起来您的对象尚未实例化。尝试:

-(void)viewDidLoad {
    [super viewDidLoad];

    CGFloat myfloat = 1.0;
    [drawRectangle setRedc:myfloat];
}

您还应该避免将对象命名为与类相同的名称作为一般编程实践。我建议将对象重命名为drawRectangle

您还需要在xib中连接drawRectangle对象

@property (nonatomic, retain) IBOutlet DrawRectangle *drawRectangle;