我有一个带有两个选项卡的选项卡式应用程序。
在第二个选项卡上,我绘制了一个图表。
但是每次我按下这个选项卡时,UIview 都不会刷新。
我试过了,
[self.view setNeedsDisplay]
inside
- (void)viewDidLoad
但还是没有刷新。
我尝试将代码从 - (void)viewDidLoad
粘贴到 -(void)viewDidAppear
但什么也没发生。
等待答复。
编辑:整个源代码
- (void)viewDidLoad
{
//[self.view setNeedsDisplay];
Sensor_VisualizationAppDelegate *delegate = (Sensor_VisualizationAppDelegate *)[[UIApplication sharedApplication] delegate];
valuePass = delegate.valuepass;
timePass = delegate.timepass;
NSNumber *value1 = [valuePass objectAtIndex:0];
NSNumber *value2 = [valuePass objectAtIndex:1];
NSNumber *value3 = [valuePass objectAtIndex:2];
NSNumber *value4 = [valuePass objectAtIndex:3];
NSNumber *value5 = [valuePass objectAtIndex:4];
NSNumber *value6 = [valuePass objectAtIndex:5];
NSNumber *value7 = [valuePass objectAtIndex:6];
NSString *time1 = [timePass objectAtIndex:0];
NSString *time2 = [timePass objectAtIndex:1];
NSString *time3 = [timePass objectAtIndex:2];
NSString *time4 = [timePass objectAtIndex:3];
NSString *time5 = [timePass objectAtIndex:4];
NSString *time6 = [timePass objectAtIndex:5];
NSString *time7 = [timePass objectAtIndex:6];
OVGraphView *graphview=[[OVGraphView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, 300) ContentSize:CGSizeMake(960, 300)];
graphview.reverse=YES;
graphview.graphcolor=[UIColor colorWithRed:0.31 green:0.73 blue:0.78 alpha:1.0];
[self.view addSubview:graphview];
[graphview setPoints:@[ [[OVGraphViewPoint alloc]initWithXLabel:time1 YValue:value1],
[[OVGraphViewPoint alloc]initWithXLabel:time2 YValue:value2],
[[OVGraphViewPoint alloc]initWithXLabel:time3 YValue:value3],
[[OVGraphViewPoint alloc]initWithXLabel:time4 YValue:value4],
[[OVGraphViewPoint alloc]initWithXLabel:time5 YValue:value5],
[[OVGraphViewPoint alloc]initWithXLabel:time6 YValue:value6],
[[OVGraphViewPoint alloc]initWithXLabel:time7 YValue:value7]
]];
[super viewDidLoad];
}
最佳答案
要检测标签栏触摸,您可以按如下方式实现 UITabBarController 委托(delegate):
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
NSLog(@"controller title: %@", viewController.title);
}
您创建标签栏的位置。然后就可以得到view controller的view实例,调用setNeedsDisplay
在 ovgraphview 的源代码中,在您调用 setPoints 之后的某处调用了此方法:
-(void)setPlotViewPoints:(NSArray *)points Reversed:(BOOL)reversebool{
if (reversebool) {
self.plotpoints=[[points reverseObjectEnumerator]allObjects];
}else{
self.plotpoints=points;
}
spacebetweenpoints=self.frame.size.width/[points count];
int d=0;
for (OVGraphViewPoint *pt in points) {
if ([pt.yvalue intValue]>d) {
d=[pt.yvalue intValue];
}
}
yscale=(self.frame.size.height-40)/d;
[self setNeedsDisplay];
}
在最后一行中,您看到它们在设置点后在 View 上调用 setNeedsDisplay,因此您无需在创建图形之前调用它。但是自从您这样做以来,这似乎与图形的内部植入混为一谈。当您调用 setNeedsDisplay 时,它会等到下一个运行循环周期才会显示,上面方法中调用的也会如此。现在我们在 View 层次结构中的不同层上调用了不止一个,这变得令人讨厌。我认为这就是问题所在。
对于您的情况,我会完全不使用 setNeedsDisplay,而是在我上面给出的委托(delegate)方法中调用 setPoints,或者如果您没有使用 tabbarcontroller 而不是实现 tabBar:didSelectItem: 选项卡栏委托(delegate)来检测您的选项卡何时被单击通过在创建标签栏项目时为其分配标签或检查其标题以验证其是否是您想要的标签,然后调用设置点。
这有点啰嗦,如果您需要我尝试进一步澄清,请告诉我。
关于ios - 在 ios 中的选项卡式应用程序中刷新 UIview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14403717/