我有这个问题,我有一个名为“GraphGenerator”的类,其他类可以调用它来传递一个 View ,从而使该 View 充满图形。 这个 GraphGenerator 类是单例的
+ (GraphGenerator *)sharedInstance
{
static GraphGenerator *sharedInstance;
static dispatch_once_t once;
dispatch_once(&once, ^{
//allochiamo la sharedInstance
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
这个类在制作图表时使用一些综合属性,如 _graphType 和 _graphData。
-(void)generateGraphInView:(CPTGraphHostingView*)hostingView ofType:(NSUInteger)type withData:(NSArray*)data andStyle:(NSUInteger)style{
_graphData=[NSMutableArray arrayWithArray:data];
_graphStyle=style;
_graphType=type;
问题是,当我在 ViewController 中的这个单例中开始多次调用时,GraphGenerator 开始制作图表,但不是一次制作图表。似乎类(class)一起做了所有的图,改变了综合的属性值并产生了问题.. 例如,我在方法中进行了这两次调用
[[GraphGenerator sharedInstance]generateGraphInView:graphHost01 ofType:DAILY withData:[_dataDictionary valueForKey:@"day share"] andStyle:BARCHART];
[[GraphGenerator sharedInstance]generateGraphInView:graphHost02 ofType:WEEKLY withData:[_dataDictionary valueForKey:@"week share"] andStyle:BARCHART];
_graphType 设置为 DAILY 紧接着它被设置为 WEEKLY,但设置得太快以至于第一次调用仍在生成他的图表,此时,生成为每周而不是每天。
那么我可以实现什么?我想到了 mutex 或类似的东西,但我不知道如何实现它。 谢谢
最佳答案
创建 UIView 的子类来管理图形创建本身将为您提供一个没有 Singleton 的解决方案。
我们之前使用 CorePlot 来实现尖峰,我们让它像这样工作。
这tutorial应该给你大部分的结构。
关于ios - 具有类变量的 Iphone sdk 共享实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17925706/