【发布时间】:2013-10-06 09:15:19
【问题描述】:
首先,让我说我是 Objective C 的新手。
我基本上是在尝试将 viewController (UIViewController) 中的 originalPriceOnGraph 变量传递给 GraphView (UIView) 中的原始变量。但是,当我尝试显示原件时,我不断得到 0.00。我不明白到底是什么问题。这是我的一些代码:
GraphView.h
@interface GraphView : UIView
@property (nonatomic) double original;
@end
GraphView.m
@implementation GraphView
@synthesize original;
- (id)initWithFrame:(CGRect)frame
{
//some code here
}
- (void)drawRect:(CGRect)rect;
{
NSLog(@"%.2f", original);
//some more code here
}
@end
ViewController.m
@interface OtherViewController ()
@end
@implementation OtherViewController
@synthesize originalPriceOnGraph;
@synthesize graph;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
originalPriceOnGraph = 20.00;
graph = [[GraphView alloc] init];
graph.original = originalPriceOnGraph;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ViewController.h
@interface OtherViewController : UIViewController
@property (strong, nonatomic) GraphView *graph;
@property (nonatomic) double originalPriceOnGraph;
@end
提前谢谢你!
编辑:我能够通过在 OtherViewController 和 GraphView 之间创建一个 IBOutlet 来解决这个问题。我还去掉了 ViewController.m 中 GraphView 的 alloc init 语句。谢谢大家的建议!
【问题讨论】:
-
有一点要确定的是,viewDidLoad中GraphView的alloc/init是GraphView的only alloc/init。在类的一个实例中设置一个值并期望它出现在另一个实例中是一个典型的新手错误。
标签: objective-c uiview uiviewcontroller